Rails 3 + Rspec 2: Ошибка проверки: электронная почта уже получена

У меня есть 2 модели: Пользователь и Корзина . Пользователь has_many Buckets ]" и Bucket own_to a Пользователь .

В factoryies.rb у меня есть:

Factory.define :user do |user|
  user.email  "teste@test.com"
  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        "user@example.com"
  bucket.confirmation false
  bucket.association :user
end

и у меня есть модуль login_user:

def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @user = Factory.create(:user)
      #@user.confirm!
      sign_in @user
    end
  end

Я использую Spork and Watch, а мой Buckets_controller_spec.rb так же прост, как:

describe "User authenticated: " do

   login_user  

   @bucket = Factory(:bucket)

   it "should get index" do
     get 'index'
     response.should be_success
   end
...
end

Ошибка всегда одна и та же:

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'

И это происходит только тогда, когда у меня есть Factory (: bucket ) . Логин работает нормально, когда я не добавляю Factory (: bucket) .

Это всегда одна и та же ошибка. Я пробовал добавить : email => Factory. next (: email) пользователю, но безуспешно.

Изменить:

В rails c test :

ruby-1.9.2-p180 :019 > bucket = Factory(:bucket, :email => "hello@hello.com")
    ActiveRecord::RecordInvalid: Validation failed: Email has already been taken

    ruby-1.9.2-p180 :018 >   Bucket.create(:email => "hello@hello.com")
     => #<Bucket id: 2, email: "hello@hello.com", confirmation: nil, created_at: "2011-04-08 21:59:12", updated_at: "2011-04-08 21:59:12", user_id: nil> 

Изменить 2:

Я обнаружил, что ошибка связана с ассоциацией, однако я не знаю, как ее исправить.

  bucket.association :user
8
задан donald 8 April 2011 в 21:59
поделиться