Monomachus
Monomachus

Reputation: 1478

rails 3 tutorial : rspec + factory_girl_rails problem

i've been following the Rails tutorial (http://railstutorial.org/chapters/beginning , Rails 3 version), and i've stopped at 11th chapter when using Factory Girl and Rspec, I have a test that isn't passing and I feel I'm doing something wrong but I don't see what.
First of all there is a git repository on Github with the code that doesn't pass that test. http://github.com/Monomachus/ch3_static_pages

So I got users model

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :microposts
  .
  .
  .


I got microposts model

class Micropost < ActiveRecord::Base
  attr_accessible :content

  belongs_to :user

  default_scope :order => 'microposts.created_at DESC'
end

Then I got Factory girl settings

Factory.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

Factory.define :micropost do |micropost|
  micropost.content "Foo bar"
  micropost.association :user
end

And finally Rspec code

require 'spec_helper'

describe Micropost do
    .
    . 
  describe "microposts associations" do

    before(:each) do
      @user = User.create(@attr)
      @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
      @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
    end

    it "should have a microposts attribute" do
      @user.should respond_to(:microposts)
    end

    it "should be in the reverse order of appearing" do
      @user.microposts.should == [@mp2, @mp1]
    end
  end
end

And I got the error which definitely tells me that I do something wrong.

Failures:

  1) Micropost microposts associations should be in the reverse order of appearing
     Failure/Error: @user.microposts.should == [@mp2, @mp1]
     expected: [#<Micropost id: 2, content: "Foo bar", user_id: nil, created_at: "2010-12-24 12:47:02", update
d_at: "2010-12-24 13:47:02">, #<Micropost id: 1, content: "Foo bar", user_id: nil, created_at: "2010-12-23 13:
47:02", updated_at: "2010-12-24 13:47:02">],
          got: [] (using ==)
     Diff:
     @@ -1,3 +1,2 @@
     -[#<Micropost id: 2, content: "Foo bar", user_id: nil, created_at: "2010-12-24 12:47:02", updated_at: "20
10-12-24 13:47:02">,
     - #<Micropost id: 1, content: "Foo bar", user_id: nil, created_at: "2010-12-23 13:47:02", updated_at: "20
10-12-24 13:47:02">]
     +[]
     # ./spec/models/micropost_spec.rb:42:in `block (3 levels) in <top (required)>'

As you can see even the user_id property is not set correctly +
apparently @user.microposts doesn't have any elements.
Please help me with this issue thanks.

Upvotes: 3

Views: 4001

Answers (3)

Monomachus
Monomachus

Reputation: 1478

Well the answer was simple :) I included microposts associations in the Micropost spec.

And clearly

describe "microposts associations" do

  before(:each) do
    @user = User.create(@attr)
    @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
    @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
  end

  it "should have a microposts attribute" do
    @user.should respond_to(:microposts)
  end

  it "should be in the reverse order of appearing" do
    @user.microposts.should == [@mp2, @mp1]
  end
end

@attr did not contain the user properties but the micropost properties and of course @user = nil and then everything makes sense. So if you do have the same problem, include this code into User spec. Now all my tests pass :)

Upvotes: 2

Mr Simple
Mr Simple

Reputation: 11

By the time I had finished the pagination chapter, the tutorial was creating 100 sample users using Faker (listing 10.25 on page 390), and in RubyMine I was able to see my test was failing because the program was throwing an exception on duplicate user email address (which has a unique constraint). The @attr on line 8 of user_spec.rb has :email => "[email protected]", however this throws an exception since it's a duplicate email (I guess because Faker has already created it).

For me the fix was to copy @attr from line 8 and paste it into the describe "micropost associations" block (user_spec.rb), and change the email address to :email => "[email protected]". I'm sure this is a total hack but I'm a n00b.

Update:

Another fix for me was to comment out the line @user = User.create(@attr), and simply create @mp1 and @mp2.

Upvotes: 1

Mark Berry
Mark Berry

Reputation: 19092

I was also getting test failure in this section, even though I already had "micropost associations" in user_spec.rb. Turns out I needed to restart spork and autotest in order to get them to use the new "micropost" factory in factories.rb.

Upvotes: 0

Related Questions