Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factory overrides are applied before other attributes. Fixes #140.
- Loading branch information
1 parent
ec88698
commit 4b83b1e
Showing
5 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'spec_helper' | ||
require 'acceptance/acceptance_helper' | ||
require 'active_support/ordered_hash' | ||
|
||
describe "attribute overrides" do | ||
before do | ||
define_model('User', :admin => :boolean) | ||
define_model('Post', :title => :string, | ||
:secure => :boolean, | ||
:user_id => :integer) do | ||
belongs_to :user | ||
|
||
def secure=(value) | ||
return unless user && user.admin? | ||
write_attribute(:secure, value) | ||
end | ||
end | ||
|
||
FactoryGirl.define do | ||
factory :user do | ||
factory :admin do | ||
admin true | ||
end | ||
end | ||
|
||
factory :post do | ||
user | ||
title "default title" | ||
end | ||
end | ||
end | ||
|
||
let(:admin) { FactoryGirl.create(:admin) } | ||
|
||
let(:post_attributes) do | ||
attributes = ActiveSupport::OrderedHash.new | ||
attributes[:secure] = false | ||
attributes | ||
end | ||
|
||
let(:non_admin_post_attributes) do | ||
post_attributes[:user] = FactoryGirl.create(:user) | ||
post_attributes | ||
end | ||
|
||
let(:admin_post_attributes) do | ||
post_attributes[:user] = admin | ||
post_attributes | ||
end | ||
|
||
context "with an admin posting" do | ||
subject { FactoryGirl.create(:post, admin_post_attributes) } | ||
its(:secure) { should == false } | ||
end | ||
|
||
context "with a non-admin posting" do | ||
subject { FactoryGirl.create(:post, non_admin_post_attributes) } | ||
its(:secure) { should be_nil } | ||
end | ||
|
||
context "with no user posting" do | ||
subject { FactoryGirl.create(:post, post_attributes) } | ||
its(:secure) { should be_nil } | ||
end | ||
end |