我有下一个代码: 我的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
Admin
和Customer
继承自User
。
现在我在行Validation failed: Email has already been taken
中出现customer = FactoryGirl.create :customer
错误。我试图清理数据库。耙db:test:prepare
也没有用。
答案 0 :(得分:2)
您的:admin
和:customer
工厂是否可以继承您的:user
工厂?如果您的:admin
和:customer
工厂被定义为这样,它是否有效?
FactoryGirl.define do
factory :admin, parent: :user do
end
factory :customer, parent: :user do
end
end