I followed the Rails tutorial (http://railstutorial.org/chapters/beginning, Rails 3 version) and I stopped in chapter 11 when using Factory Girl and Rspec, I have a test that fails, and I feel that I’m doing something wrong, but I don’t see that.
First of all, there is a git repository on Github with code that fails this test.
http://github.com/Monomachus/ch3_static_pages
So, I got a user model
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
has_many :microposts
.
.
.
I got a microflow 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 "mhartl@example.com"
user.password "foobar"
user.password_confirmation "foobar"
end
Factory.define :micropost do |micropost|
micropost.content "Foo bar"
micropost.association :user
end
And finally, the 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 an error that definitely tells me that I am doing something wrong.
Failures:
1) Micropost microposts associations should be in the reverse order of appearing
Failure/Error: @user.microposts.should == [@mp2, @mp1]
expected: [
d_at: "2010-12-24 13:47:02">,
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)>'
, user_id +
- @user.microposts .
, .