Rails: Minitest 测试在应该通过时失败了
Rails: Minitest tests fail when they should pass
按照 Michael Hartl 的 Rails 教程,我正在 Rails.
中使用 Minitest 测试静态页面的 <title>
这是 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.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>..
我错过了什么?
更新:我继续学习教程并遵循高级测试设置部分中的指南。
这是我 运行 使用 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
我一直在调查问题的原因,但到目前为止没有找到任何结论。
有什么想法吗?
我不明白你对竖线字符的评论,不在 <%= %> 之间。因为:您的文件没有使用 erb 测试 ''yielded' 标题。这将在本教程的后面发生。
在这个测试中,你应该测试 'simple' html 格式。
这可能是问题所在吗?
我记不清这次测试的具体内容了,但会不会是你的路由问题?
因此,我一直按照教程进行操作,在下一章(第 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
如果您遇到同样的问题,希望对您有所帮助。
按照 Michael Hartl 的 Rails 教程,我正在 Rails.
中使用 Minitest 测试静态页面的<title>
这是 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.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>..
我错过了什么?
更新:我继续学习教程并遵循高级测试设置部分中的指南。
这是我 运行 使用 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
我一直在调查问题的原因,但到目前为止没有找到任何结论。
有什么想法吗?
我不明白你对竖线字符的评论,不在 <%= %> 之间。因为:您的文件没有使用 erb 测试 ''yielded' 标题。这将在本教程的后面发生。 在这个测试中,你应该测试 'simple' html 格式。 这可能是问题所在吗?
我记不清这次测试的具体内容了,但会不会是你的路由问题?
因此,我一直按照教程进行操作,在下一章(第 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
如果您遇到同样的问题,希望对您有所帮助。