I have following view component in my rails application
class Elements::FlashComponent < ViewComponent::Base
def initialize(*)
super
end
def flash_class(level)
case level
when "notice"
"alert alert-info"
when "success"
"alert alert-success"
when "error"
"alert alert-danger"
when "alert"
"alert alert-warning"
end
end
end
and i'd like to test it with rspec test
require "rails_helper"
RSpec.describe Elements::FlashComponent, type: :component do
it "renders flash notification" do
with_controller_class Customer::DashboardController do
flash.now[:notice] = "Notification message!"
render_inline described_class.new()
end
expect(rendered_component).to have_text "Notification message!"
expect(rendered_component).to have_selector "label"
expect(rendered_component).not_to have_selector "img"
end
end
But every time I run tests I see the following message
NameError: undefined local variable or method `flash' for #<RSpec::ExampleGroups::ElementsFlashComponent "renders flash notification"
how to fix it?
CodePudding user response:
Finally, i've fixed it adding controller. before flash:
with_controller_class Customer::DashboardController do
controller.flash[:notice] = "Notification message!"
render_inline described_class.new()
end
