2015-06-23 42 views
0

繼邁克爾哈特爾的Rails教程,我測試我的靜態頁面的<title> Minitest在Rails中。Rails:Minitest測試失敗時,他們應該通過

這裏是static_pages_controller_test.rb文件:

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 

這裏是主頁home.html.erb文件:

<!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.erbabout.html.erb基本上包含相同的內容,有細微的不同。

因此,從我的理解,測試應該通過。

然而,當我運行rake,我得到:

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 

特別是,我不明白爲什麼我得到:

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

我缺少什麼?

更新:我去上的教程,跟着在先進的測試設置部分的準則。

這裏是我所得到的,當我運行所有測試與後衛:

[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 

我一直在調查問題的原因,但至今沒有發現任何確鑿。

有什麼想法?

+0

真的我不知道,但你有沒有嘗試更換管道字符?或逃脫它? – rfellons

+0

@fellons這是一個很好的評論,我沒有想過。但是我只是看了一下這些文件,結果發現管道字符不在<%= ... %>標記中。無論如何我用短劃線字符替換它,但得到相同的測試結果。我也逃脫了,但測試仍然沒有通過。真奇怪。這可能是由於我使用的寶石版本。 –

+0

也許在這裏你可以找到如何調試最新情況。 https://coderwall.com/p/bbxb8g/use-ruby-debugger-when-debugging-rspecs – rfellons

回答

0

因此,我一直遵循教程,並在下一章(第4章)中,在某些時候,我們創建full_title幫助程序並從視圖中刪除<%provide(:title,「Home」)%> 。

我不知道怎麼樣,但是這解決了這個問題,並做出測試通過:

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 

希望,如果你偶然發現了同樣的問題沒有什麼幫助。

0

我不明白你對管道字符的評論,不在<%=%>之間。因爲:你的文件沒有用erb來測試''已取得''的標題。這在教程後面會發生。 在這個測試中,你應該測試'簡單'的html格式。 這是什麼問題?

我不太清楚這個測試是關於什麼的,但它可能是你的路由選擇的東西?

+0

這個測試只是關於我們在家中是否有標籤,關於和幫助頁面,以及這些<title>標籤是否分別包含「Home | Microblog」,「About | Microblog」和「Help | Microblog」。微博是應用程序的名稱。在本教程中,我們正在測試「Home | Ruby on Rails教程示例應用程序」,而不是「Home | Microblog」等。我實施了必要的更改以測試正確的文本。 – <span class="text-secondary"> <small> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">我對管道字符的評論基於以下原因:在app/views/static_pages/home.html.erb文件中,我們有<title><%= yield(:title)%> |微博而不是<%= yield(:title)|微博%>所以我很困惑從簡單的HTML代碼轉義管道。無論如何,我試過並沒有解決這個問題。它更有意義嗎? –

相關問題