我的 Rails 應用程式中有以下視圖組件
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
我想用 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
但是每次我運行測驗時,我都會看到以下訊息
NameError: undefined local variable or method `flash' for #<RSpec::ExampleGroups::ElementsFlashComponent "renders flash notification"
如何解決?
uj5u.com熱心網友回復:
最后,我已經修復了它controller.在 flash 之前添加:
with_controller_class Customer::DashboardController do
controller.flash[:notice] = "Notification message!"
render_inline described_class.new()
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411421.html
標籤:
