Rails教程 - 第12.2.3节集成测试失败 - 预期为真

时间:2015-04-10 18:32:29

标签: ruby-on-rails railstutorial.org

我一直在讨论这个问题。我甚至复制并粘贴了教程中的示例,所以我不确定我做错了什么。我很感激任何帮助。

到目前为止,我通过了所有测试。

我创建了集成测试before listing 12.27

$ rails generate integration_test following

这是我的test / fixtures / relationships.yml文件(我在整个教程中一直使用mochi代替michael)。

one:
  follower: mochi
  followed: lana

two:
  follower: mochi
  followed: mallory

three:
  follower: lana
  followed: mochi

four:
  follower: archer
  followed: mochi

这是我的test / integration / following_test.rb

require 'test_helper'

class FollowingTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:mochi)
    log_in_as(@user)
  end

  test "following page" do
    get following_user_path(@user)
    assert_not @user.following.empty?
    assert_match @user.following.count.to_s, response.body
    @user.following.each do |user|
      assert_select "a[href=?]", user_path(user)
    end
  end

  test "followers page" do
    get followers_user_path(@user)
    assert_not @user.followers.empty?
    assert_match @user.followers.count.to_s, response.body
    @user.followers.each do |user|
      assert_select "a[href=?]", user_path(user)
    end
  end
end

这是我运行bundle exec rake test时遇到的测试失败

$ bundle exec rake test
Run options: --seed 23507

# Running:

........................................................FF

Finished in 1.736838s, 33.3940 runs/s, 164.0913 assertions/s.

  1) Failure:
FollowingTest#test_following_page [/app_path/test/integration/following_test.rb:12]:
Expected true to be nil or false


  2) Failure:
FollowingTest#test_followers_page [/app_path/test/integration/following_test.rb:21]:
Expected true to be nil or false

58 runs, 285 assertions, 2 failures, 0 errors, 0 skips

如果有人对可能造成这种失败的原因有任何想法,我会非常感激。这让我很疯狂。

2 个答案:

答案 0 :(得分:1)

@user.following.empty?

...正在返回true。但你期望它是零或假。

  

assert_not - 断言表达式不是真的。如果object为nil或false,则通过。

来自here

简而言之,@ user.following是空的。您的测试期望

答案 1 :(得分:0)

检查您是否正确设置了用户灯具。如果您没有关系设备所需的用户,那么它就不会起作用。

检查Michael Hartl的GitHub用户灯具,看看你是否拥有它们: https://github.com/mhartl/sample_app_3rd_edition/blob/master/test/fixtures/users.yml

为我工作。

相关问题