I have 2 models, User
and Bucket
. User
has_many
Buckets
and a Bucket
belongs_to
a User
.
In factories.rb
, I have:
Factory.define :user do |user|
user.email "[email protected]"
user.password "foobar"
user.password_confirmation "foobar"
end
Factory.sequence :email do |n|
"person-#{n}@example.com"
end
Factory.define :bucket do |bucket|
bucket.email "[email protected]"
bucket.confirmation false
bucket.association :user
end
and I have a login_user module as follows:
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
@user = Factory.create(:user)
#@user.confirm!
sign_in @user
end
end
I am using Spork and Watch and my Buckets_controller_spec.rb
is as simple as:
describe "User authenticated: " do
login_user
@bucket = Factory(:bucket)
it "should get index" do
get 'index'
response.should be_success
end
...
end
The error is always the same:
Failures:
1) BucketsController User authenticated: should get index
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./lib/controller_macros.rb:12:in `block in login_user'
And it only happens when I have the Factory(:bucket)
. The login works fine when I don't add the Factory(:bucket)
.
It's always the same error. I have tried adding :email => Factory.next(:email)
to the user, but no success.
Edit:
In rails c test
:
ruby-1.9.2-p180 :019 > bucket = Factory(:bucket, :email => "[email protected]")
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
ruby-1.9.2-p180 :018 > Bucket.create(:email => "[email protected]")
=> #<Bucket id: 2, email: "[email protected]", confirmation: nil, created_at: "2011-04-08 21:59:12", updated_at: "2011-04-08 21:59:12", user_id: nil>
Edit 2:
I found out that the error is in the association, however, I don't know how to fix it.
bucket.association :user