简体   繁体   中英

Michael Hartl's Rails tutorial chapter 12 — undefined method `users'

I'm working on Michal Hartl's tutorial and I'm getting error after first test.

E

Error:
RelationshipTest#test_should_be_valid:
NoMethodError: undefined method `users' for #<RelationshipTest:0x00007feb7da11100>
    test/models/relationship_test.rb:6:in `setup'


bin/rails test test/models/relationship_test.rb:10

E

Error:
RelationshipTest#test_should_require_a_followed_id:
NoMethodError: undefined method `users' for #<RelationshipTest:0x00007feb7dadf2a8>
    test/models/relationship_test.rb:6:in `setup'


bin/rails test test/models/relationship_test.rb:19

E

Error:
RelationshipTest#test_should_require_a_follower_id:
NoMethodError: undefined method `users' for #<RelationshipTest:0x00007feb7e8d04e8>
    test/models/relationship_test.rb:6:in `setup'


bin/rails test test/models/relationship_test.rb:14

Here is my relationship_test.rb:

require 'test_helper'

class RelationshipTest < ActiveSupport::TestCase

  def setup
    @relationship = Relationship.new(follower_id: users(:michael).id,
                                    followed_id: users(:archer).id)
  end

  test "should be valid" do
    assert @relationship.valid?
  end

  test "should require a follower_id" do
    @relationship.follower_id = nil
    assert_not @relationship.valid?
  end

  test "should require a followed_id" do
    @relationship.followed_id = nil
    assert_not @relationship.valid?
  end
end

I'm really confused by error, so any help would be appreciated. I've tried to search other answers, but couldn't find anything on this.

You need to create and put some fixtures in test/fixtures/users.yml .

Example (you probably need to adapt it to your own database design):

michael:
  email: "user_1@example.com"
  uid: "user_1@example.com"
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>

This fixture would now be accesible in your test as users(:michael) . Check the Rails docs for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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