thoughtbot/factory_girl — Issue #272: Traits aren't applied when building models
Author: stevenharman
Created: 2012-01-17 23:09:08+00:00
State: open

This was mentioned in issue 270, and I'm still seeing it after the 2.4.1 release. An example:

```ruby
# spec/factories/beers.rb
FactoryGirl.define do
factory :beer do
brewery
status :stocked
best_by { (30..90).to_a.shuffle.first.days.from_now }

trait(:drunk) { status :drunk }
trait(:traded) { status :traded }
trait(:skunked) { status :skunked }
end
end

# spec/models/beer_spec.rb
describe ".scopes for status" do
before do
FactoryGirl.create(:beer)
FactoryGirl.create(:beer, :drunk)
FactoryGirl.create(:beer, :skunked)
end

it ".stocked only returns stocked beers" do
beers = Beer.stocked.all
beers.should have(1).beer # FAIL: returns all 3
end

it ".drunk only returns drunk beers" do
beers = Beer.drunk.all
beers.should have(1).beer # FAIL: returns all 3
end

end
```

However, changing the before block to specify the status directly works:

```ruby
before do
FactoryGirl.create(:beer)
FactoryGirl.create(:beer, :status => :drunk)
FactoryGirl.create(:beer, :status => :skunked)
end
```