在Rails应用程序中测试失败

时间:2011-12-03 21:09:32

标签: ruby-on-rails ruby testing

我正在按照书籍Simply Rails 2中的教程构建应用程序,并且无法通过某些测试。

以下是我收到的错误:

  1) Failure:
test_should_show_index(StoriesControllerTest)
[/Users/ryanclark/Projects/shovell/test/functional/stories_controller_test.rb:8:in `test_should_show_index'
 /Users/ryanclark/.rvm/gems/ruby-1.8.6-p420/gems/activesupport-2.0.2/lib/active_support/testing/default.rb:7:in `run']:
<nil> expected to not be nil.

  2) Failure:
test_should_show_navigation_menu(StoriesControllerTest)
[/Users/ryanclark/.rvm/gems/ruby-1.8.6-p420/gems/actionpack-2.0.2/lib/action_controller/assertions/selector_assertions.rb:297:in `assert_select'
 /Users/ryanclark/Projects/shovell/test/functional/stories_controller_test.rb:44:in `test_should_show_navigation_menu'
 /Users/ryanclark/.rvm/gems/ruby-1.8.6-p420/gems/activesupport-2.0.2/lib/active_support/testing/default.rb:7:in `run']:
Expected at least 2 elements matching "#navigation li", found 3.
<false> is not true.

  3) Failure:
test_should_show_new_form(StoriesControllerTest)
[/Users/ryanclark/.rvm/gems/ruby-1.8.6-p420/gems/actionpack-2.0.2/lib/action_controller/assertions/selector_assertions.rb:297:in `assert_select'
 /Users/ryanclark/Projects/shovell/test/functional/stories_controller_test.rb:18:in `test_should_show_new_form'
 /Users/ryanclark/.rvm/gems/ruby-1.8.6-p420/gems/activesupport-2.0.2/lib/active_support/testing/default.rb:7:in `run']:
Expected at least 3 elements matching "form p", found 4.
<false> is not true.

我的项目在Github,我真的很感激,如果有人想看看并帮助我,我会非常感激。

此外,我很乐意提供更多详细信息和代码,只是为了保持帖子简短。

1 个答案:

答案 0 :(得分:1)

失败1:在测试'test_should_show_index'第8行assert_not_nil assigns(:stories)

您的测试数据库中没有'votes_count&gt; = 5'的商店。这是Stories(stories.yml)的固定装置:

one:
  name: My shiny weblog 
  link: http://poocs.net
  user: patrick

two:
  name: SitePoint Forums
  link: http://www.sitepoint.com/forums/
  user: patrick 

所有故事都有voting_count的默认值(它为0,请参阅db / schema.rb,第20行)。你需要至少一个故事才能让votes_count&gt; = 5,从而将夹具改成这样的东西:

one:
  name: My shiny weblog 
  link: http://poocs.net
  user: patrick

two:
  name: SitePoint Forums
  link: http://www.sitepoint.com/forums/
  user: patrick 
  votes_count: 5

失败2:在测试'test_should_show_navigation_menu'第44行assert_select '#navigation li', 2

您的导航div有3个子<li>标签,而不是2个预期标签(请参阅application.html.erb):

<ul id="navigation">
    <li><%= link_to 'Front page stories', stories_path %></li>
    <li><%= link_to 'Upcoming stories', bin_stories_path %></li>
    <li><%= link_to 'Submit a new story!', new_story_path %></li>
</ul>

失败3:在第18行assert_select 'form p', :count => 3的测试'test_should_show_new_form'中。

您的表单包含4个<p>标记,而不是3个标记。您可以在模板中找到“新”操作方法(new.html.erb):

<% form_for(@story) do |f| %>
  <p>
    <b>Name</b><br />
    <%= f.text_field :name %>
  </p>

  <p>
    <b>Link</b><br />
    <%= f.text_field :link %>
  </p>

    <p>
        description:<br />
        <%= f.text_area :description %>
    </p>

  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

希望这有帮助。