Question

I have next code: My 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

In my sequences:

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

And my factories:

FactoryGirl.define do
  factory :admin do
  end
end

And

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

And

FactoryGirl.define do
  factory :customer do
  end
end

And

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 and Customer inherited from User. Now I have Validation failed: Email has already been taken error in line customer = FactoryGirl.create :customer. I've tried to clean database. rake db:test:prepare didn't work too.

Was it helpful?

Solution

Can it be that your :admin and :customer factories does not inherit from your :user factory? Does it work if your :admin and :customer factories are defined like this?

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

  factory :customer, parent: :user do
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top