Rails: Minitest tests fail when they should pass

629 views Asked by At

Following the Rails Tutorial by Michael Hartl, I am testing for the <title> of my static pages with Minitest in Rails.

Here is the 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", "Home | Microblog"
  end

  test "should get help" do
    get :help
    assert_response :success
    assert_select "title", "Help | Microblog"
  end

  test "should get about" do
    get :about
    assert_response :success
    assert_select "title", "About | Microblog"
  end

end

Here is the Home page home.html.erb file:

<!DOCTYPE html>
<html>
  <head>
    <title>Home | Microblog</title>
  </head>
  <body>
    <h1>Microblog</h1>
    <p>
        This is the homepage for Microblog, a brand new microblogging app.
    </p>
  </body>
</html>

help.html.erb and about.html.erb basically contain the same content, with slight variations.

So, from my understanding, tests should pass.

However, when I run rake, I get:

Run options: --seed 47355

# Running:

FFF

Finished in 0.112314s, 26.7109 runs/s, 53.4217 assertions/s.

  1) Failure:
StaticPagesControllerTest#test_should_get_home [/Users/TXC/code/microblog/test/controllers/static_pages_controller_test.rb:7]:
<Home | Microblog> expected but was
<Home | Microblog>..
Expected 0 to be >= 1.


  2) Failure:
StaticPagesControllerTest#test_should_get_help [/Users/TXC/code/microblog/test/controllers/static_pages_controller_test.rb:13]:
<Help | Microblog> expected but was
<Help | Microblog>..
Expected 0 to be >= 1.


  3) Failure:
StaticPagesControllerTest#test_should_get_about [/Users/TXC/code/microblog/test/controllers/static_pages_controller_test.rb:19]:
<About | Microblog> expected but was
<About | Microblog>..
Expected 0 to be >= 1.

3 runs, 6 assertions, 3 failures, 0 errors, 0 skips

In particular, I don't understand why I get:

<Home | Microblog> expected but was
    <Home | Microblog>..

What am I missing?

UPDATE: I went on with the tutorial and followed the guidelines in the Advanced testing setup section.

Here is what I get when I run all the tests with Guard:

[1] guard(main)> 
11:41:17 - INFO - Run all
11:41:17 - INFO - Running: all tests
Started

 FAIL["test_should_get_home", StaticPagesControllerTest, 2015-06-20 19:36:39 -0700]
 test_should_get_home#StaticPagesControllerTest (1434854199.36s)
        <Home | Microblog> expected but was
        <Home | Microblog>..
        Expected 0 to be >= 1.
        test/controllers/static_pages_controller_test.rb:7:in `block in <class:StaticPagesControllerTest>'

 FAIL["test_should_get_help", StaticPagesControllerTest, 2015-06-20 19:36:39 -0700]
 test_should_get_help#StaticPagesControllerTest (1434854199.37s)
        <Help | Microblog> expected but was
        <Help | Microblog>..
        Expected 0 to be >= 1.
        test/controllers/static_pages_controller_test.rb:13:in `block in <class:StaticPagesControllerTest>'

 FAIL["test_should_get_about", StaticPagesControllerTest, 2015-06-20 19:36:39 -0700]
 test_should_get_about#StaticPagesControllerTest (1434854199.38s)
        <About | Microblog> expected but was
        <About | Microblog>..
        Expected 0 to be >= 1.
        test/controllers/static_pages_controller_test.rb:19:in `block in <class:StaticPagesControllerTest>'

  3/3: [===================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.22498s
3 tests, 6 assertions, 3 failures, 0 errors, 0 skips

I kept investigating the causes of the problem but did not find anything conclusive so far.

Any idea?

2

There are 2 answers

0
Thibaud Clement On BEST ANSWER

So, I kept following the tutorial, and in the next chapter (Chapter 4), at some point, we create the full_title helper and remove the <% provide(:title, "Home") %> from the views.

I am not sure how, but this solved the problem and made the tests to pass:

11:49:36 - INFO - Running: test/controllers/static_pages_controller_test.rb
Started

 FAIL["test_should_get_home", StaticPagesControllerTest, 2015-06-20 19:36:39 -0700]
 test_should_get_home#StaticPagesControllerTest (1434854199.39s)
        <Microblog> expected but was
        <Home | Microblog>..
        Expected 0 to be >= 1.
        test/controllers/static_pages_controller_test.rb:7:in `block in <class:StaticPagesControllerTest>'

  3/3: [===================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.21526s
3 tests, 6 assertions, 1 failures, 0 errors, 0 skips


11:50:15 - INFO - Running: test/controllers/static_pages_controller_test.rb
Started

  3/3: [===================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.21126s
3 tests, 6 assertions, 0 failures, 0 errors, 0 skips

Hope that helps if you stumble upon the same issue.

2
Mauddev On

I don't understand your comment about the pipe character, not being between <%= %>. Because: your files are not testing the ''yielded' titles with the erb . That happens later in the tutorial. In this test, you should test the 'simple' html format. Any chance this is the problem?

I don't remember exactly what this test was about, but could it be something with your routing?