in43sh
in43sh

Reputation: 921

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.

Upvotes: 0

Views: 143

Answers (1)

edwardmp
edwardmp

Reputation: 6601

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: "[email protected]"
  uid: "[email protected]"
  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.

Upvotes: 2

Related Questions