我有一个带有“事件”模型的 Rails 应用程序,在“spec/features”目录中,我有一个“list_events_spec.rb”文件和一个“show_event_spec.rb”文件。以下 3 个断言在放入“list_events”规范时使我的 RSpec 测试崩溃,但是当我将断言放入“show_event”规范时测试通过了。页眉、页脚和旁白都是在 application.html.erb 文件中呈现的部分内容。
我唯一能想到的是,Rails 根据文件名对这两个文件进行不同的处理,但我希望断言不管它们放在哪个 spec_file 中都可以工作,因为它们是在 application.html.erb 文件中呈现的.
以下是断言(文件名是“_header.html.erb”、“_footer.html.erb”和“_sidebar.html.erb”):
it "has a header" do
expect(page).to have_selector("header")
end
it "has a footer" do
expect(page).to have_selector("footer")
end
it "has a sidebar" do
expect(page).to have_selector("aside")
end
这是 application.html.erb 文件:
<!DOCTYPE html>
<html>
<head>
<title>EventsTdd</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'header' %>
<%= render 'sidebar' %>
<%= yield %>
</body>
<%= render 'footer' %>
</html>
这是 RSpec 错误消息:
Failures:
1) viewing the list of events has a sidebar
Failure/Error: expect(page).to have_selector("aside")
Capybara::ExpectationNotMet:
expected to find css "aside" but there were no matches
# ./spec/features/list_events_spec.rb:53:in `block (2 levels) in <top (required)>'
2) viewing the list of events has a footer
Failure/Error: expect(page).to have_selector("footer")
Capybara::ExpectationNotMet:
expected to find css "footer" but there were no matches
# ./spec/features/list_events_spec.rb:49:in `block (2 levels) in <top (required)>'
3) viewing the list of events has a header
Failure/Error: expect(page).to have_selector("header")
Capybara::ExpectationNotMet:
expected to find css "header" but there were no matches
# ./spec/features/list_events_spec.rb:45:in `block (2 levels) in <top (required)>'
Finished in 0.8435 seconds
33 examples, 3 failures
Failed examples:
rspec ./spec/features/list_events_spec.rb:52 # viewing the list of events has a sidebar
rspec ./spec/features/list_events_spec.rb:48 # viewing the list of events has a footer
rspec ./spec/features/list_events_spec.rb:44 # viewing the list of events has a header
Randomized with seed 16198