ruby on rails - "Name has already been taken" in RSpec/FactoryGirl with multiple associations -


i trying run basic spec test , fails error "name has been taken".

update belongs user has many roles.

user model

# == schema information # # table name: users # #  id                     :integer          not null, primary key #  email                  :string           default(""), not null #  factorygirl.define   factory :user_engineer, class: user     id 1     email 'someone@somewhere.com'     roles {[factorygirl.create(:engineer)]}   end end 

roles model

# == schema information # # table name: roles # #  id          :integer          not null, primary key #  name        :string #  description :text #  factorygirl.define   factory :engineer, class: role     id 3     name 'engineer'     description 'he chosen one'   end end 

updates model

# == schema information # # table name: updates # #  id            :integer          not null, primary key #  content       :text #  user_id       :integer #  ticket_id :integer #  factorygirl.define   factory :update     content "this test update"     association :user, factory: :user_engineer   end end 

update_spec.rb

require 'rails_helper'  rspec.describe update, type: :model   let(:update){ factorygirl.create :update }   { expect(update).to be_valid } end 

this error:

update   example @ ./spec/models/update_spec.rb:19 (failed - 1)  failures:    1) update       failure/error: roles {[factorygirl.create(:engineer)]}       activerecord::recordinvalid:        validation failed: name has been taken 

how can pass tests?!

edit: adding sequence line suggested, following error after running rails_env=test rake db:drop:

1) update       failure/error: roles {[factorygirl.create(:engineer)]}       activerecord::recordnotunique:        pg::uniqueviolation: error:  duplicate key value violates unique constraint "roles_pkey"        detail:  key (id)=(3) exists.        : insert "roles" ("id", "name", "description", "created_at", "updated_at") values ($1, $2, $3, $4, $5) returning "id" 

as error clear have uniq validation on name attribute, should use sequence technique.

factorygirl.define   factory :engineer, class: role     id 3     sequence(:name) { |n| "engineer-#{n}" }     description 'he chosen one'   end end 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -