Failure: Expected 0 to be >= 1 on ruby on rails

我们两清 提交于 2019-11-30 09:19:37

问题


I'm doing Hartle tutorial and see this failure every time I run rake test I see this failure:

  1) Failure:
StaticPagesControllerTest#test_should_get_help [.../sample_app/test/controllers/static_pages_controller_test.rb:14]:
<Help | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.

What does it mean? And how can I solve it? This is my static_pages_controller_test.rb file.

require 'test_helper'

class StaticPagesControllerTest < ActionController::TestCase

  test "should get home" do
    get :home
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"   end

  test "should get help" do
    get :help
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App"   end

  test "should get about" do
    get :about
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"   end

  test "should get contact" do
    get :contact
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"   end end

And here is line 14.

assert_select "title", "Help | Ruby on Rails Tutorial Sample App"

回答1:


The issue is that there is no html matching "Help | Ruby on Rails Tutorial Sample App".

if you look at the definition of assert_select, it accepts :count as (optional) argument. If the count is not specified, it sets the minimum occurrence of the html to 1. This is why the error you are getting is Expected 0 to be >= 1.. In your case there were 0 matches where the test expected at least 1 match.




回答2:


I ran across this same problem too. It came from a copy/paste of the html views from the tutorial.

Even if the text is the same between your view and your test, if you copy/paste from the Rails tutorial, you should re-write the text between title tags in your view (Home, About, Help, etc) yourself and the text should pass. Hope this help, it passed for me with this.

I'm using vim. Don't know if it matters.




回答3:


I ran across this same problem. What I did was take the | Ruby on Rails Tutorial Sample App part off, inside the end of the <title> tag in your application.html.erb file. Hope this helps!




回答4:


I had the same problem with three failures, home, about and help.

It turned out to be a simple typo in static_pages/home(about & help).html.erb I had misspelt "Tutorial" and copied pasted the same mistake into each html.erb. Corrected typo and re-ran rails test. Success :)




回答5:


Put this <% provide(:title, "Home") %> in static_pages/(home/about/contact).html.erb file. I hope this solve your problem



来源:https://stackoverflow.com/questions/29945472/failure-expected-0-to-be-1-on-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!