为什么我的RSpec断言在一个视图规范而不是另一个视图规范上工作?

时间:2013-08-10 21:32:00

标签: ruby-on-rails rspec

我有一个带有“事件”模型的Rails应用程序,在'spec / features'目录中,我有一个'list_events_spec.rb'文件和一个'show_event_spec.rb'文件。当放入'list_events'规范时,以下3个断言使我的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

0 个答案:

没有答案