简体   繁体   English

Rails教程-第12.2.3章集成测试失败-预期为True

[英]Rails Tutorial - Chapter 12.2.3 Integration Test Failure - Expected True

I have been racking my brain on this for a while. 我已经为此花了很长时间。 I even copy and pasted the examples from the tutorial, so I am not sure what I am doing wrong. 我什至复制并粘贴了教程中的示例,所以我不确定自己做错了什么。 I would appreciate any help with this. 我将不胜感激。

I passed all of my tests up to this point. 到目前为止,我通过了所有测试。

I created the integration test just before listing 12.27 在清单12.27之前创建了集成测试

$ rails generate integration_test following

Here is my test/fixtures/relationships.yml file (I have been using mochi instead of michael the whole tutorial). 这是我的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

Here is my test/integration/following_test.rb 这是我的测试/整合/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

And here is the test failure I am getting when I run bundle exec rake test 这是我运行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

If anyone has any ideas as to what could be causing this failure, I would really appreciate it. 如果有人对导致此故障的原因有任何想法,我将非常感激。 This has been driving me crazy. 这让我发疯。

@user.following.empty?

...is returning true. ...返回真。 But you're expecting it to be nil or false. 但是您期望它为零或错误。

assert_not - Assert that an expression is not truthy. assert_not-断言表达式不真实。 Passes if object is nil or false. 如果object为nil或false,则通过。

from here . 这里

In short, @user.following IS empty. 简而言之,@ user.following是空的。 Your test is expecting it not to be. 您的测试预期不会如此。

Check that you have properly set your User fixtures. 检查您是否正确设置了用户装置。 If you don't have users needed for the relationships fixtures, then it simply won't work. 如果您不需要用户来使用关系装置,那么它将根本无法使用。

Check Michael Hartl's GitHub User fixtures and see if you have them all: https://github.com/mhartl/sample_app_3rd_edition/blob/master/test/fixtures/users.yml 检查Michael Hartl的GitHub User固定装置,看看是否全部拥有: https : //github.com/mhartl/sample_app_3rd_edition/blob/master/test/fixtures/users.yml

Worked for me. 为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM