FactoryGirl验证失败:已收到电子邮件

时间:2014-04-02 08:02:43

标签: ruby-on-rails ruby ruby-on-rails-4 factory-bot

我有下一个代码: 我的users_controller_test.rb:

require 'test_helper'

class Api::Be::UsersControllerTest < ActionController::TestCase

  setup do
    admin = FactoryGirl.create :admin

    store = FactoryGirl.create(:store, admin: admin)
    store.ug_default = UgDefault.new
    store.save

    customer = FactoryGirl.create :customer
    customer.store_id = store.id
    customer.save
  end

  test "should show user" do
    get :show, store_id: store.id, u: admin.u_token
    assert_response :success

    json = response.body
    data = JSON.parse(json)

    assert_equal 'ok', data['status']
  end

end

在我的序列中:

sequence :email do |n|
    "email-#{n}@example.com"
  end

我的工厂:

FactoryGirl.define do
  factory :admin do
  end
end

FactoryGirl.define do
  factory :store do
    admin
    name {generate :string}
    subdomain {generate :string}
    linked_domain {generate :string}
  end
end

FactoryGirl.define do
  factory :customer do
  end
end

FactoryGirl.define do
  factory :user do
    first_name {generate :string}
    last_name {generate :string}
    email {generate :email}
    password_digest {generate :password_digest}
    login_attempts 1
    company_name {generate :string}
    phone {generate :string}
    u_token 'u'
  end
end

AdminCustomer继承自User。 现在我在行Validation failed: Email has already been taken中出现customer = FactoryGirl.create :customer错误。我试图清理数据库。耙db:test:prepare也没有用。

1 个答案:

答案 0 :(得分:2)

您的:admin:customer工厂是否可以继承您的:user工厂?如果您的:admin:customer工厂被定义为这样,它是否有效?

FactoryGirl.define do
  factory :admin, parent: :user do
  end

  factory :customer, parent: :user do
  end
end