Skip to content

Instantly share code, notes, and snippets.

@IamNaN
Created April 24, 2012 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IamNaN/2484336 to your computer and use it in GitHub Desktop.
Save IamNaN/2484336 to your computer and use it in GitHub Desktop.
Unexpected results with rspec and factory girl
require 'spec_helper'
describe Article do
it 'can set the privacy' do
article = create(:article)
article.comments.first.privacy.should eq 'low' #Pass
comment = article.comments.first
comment.privacy.should eq 'low' #Pass
comment.privacy = 'high'
comment.privacy.should eq 'high' #Pass
article.comments.first.privacy = 'high'
article.comments.first.privacy.should eq 'high' #Fail: expected 'high' got 'low'
end
end
class Article < ActiveRecord::Base
has_many :comments
attr_accessible :content, :title
end
class Comment < ActiveRecord::Base
belongs_to :article
attr_accessible :content, :privacy
end
FactoryGirl.define do
factory :article do
sequence(:title) { |n| "Article ##{n}"}
content 'Lorem ipsum'
after_create do |article, eval|
FactoryGirl.create_list(:comment, 1, article: article)
end
end
factory :comment do
privacy 'low'
content 'dolor sit amet.'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment