Rails:Minitest测试应该通过时失败

时间:2015-06-23 14:57:18

标签: ruby-on-rails testing tdd railstutorial.org minitest

按照Michael Hartl的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.erb

about.html.erbrake基本上包含相同的内容,但略有不同。

因此,根据我的理解,测试应该通过。

然而,当我运行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

我错过了什么?

更新:我继续学习本教程并遵循高级测试设置部分中的指南。

以下是我使用Guard运行所有测试时得到的结果:

{{1}}

我一直在调查问题的原因,但到目前为止还没有找到任何结论。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我不理解您对竖线字符的评论,而不是介于&lt;%=%&gt;之间。因为:您的文件没有测试&#39;&#39;产生&#39;与erb的标题。这将在本教程后面发生。 在这个测试中,你应该测试简单的&#39; HTML格式。 这有什么问题吗?

我不记得这个测试到底是什么,但是它可能与你的路由有关吗?

答案 1 :(得分:0)

所以,我一直关注本教程,在下一章(第4章)中,我们在某个时候创建​​了full_title帮助器并删除了&lt;%provide(:title,&#34; Home& #34;)%&gt;从意见。

我不确定如何,但这解决了问题并使测试通过:

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

希望如果你遇到同样的问题会有所帮助。