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.
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
Related
In my application an account can have a single owner (user) and multiple users.
In my tests I do this:
# account_factory_static.rb
FactoryGirl.define do
factory :account do
name 'demoaccount'
association :owner, :factory => :user
end
end
# user_factory_static.rb
FactoryGirl.define do
factory :user do
email 'demo#example.com'
first_name 'Jon'
last_name 'Doe'
password 'password'
end
end
and use them like below:
let(:account) { FactoryGirl.create(:account) }
The problem is that right nowaccount.users.count equals 0 because I have no way to do something like #account.users << #account.owner like I do in my controllers when a user signs up.
The question is how can I add the associated account's id to the account_id attribute of the user in FactoryGirl?
In other words how do you do it in FactoryGirl?
Thanks.
You can use after :create block for it:
FactoryGirl.define do
factory :account do
name 'demoaccount'
association :owner, :factory => :user
after :create do |account|
account.users << account.owner
end
end
end
I created factory client and contract. I run test, but display error
FactoryGirl.define do
factory :client, class: User do
role 'client'
first_name 'John'
sequence(:last_name) { |n| "client#{n}" }
sequence(:email) { |n| "client#{n}#example.com" }
# avatar { Rack::Test::UploadedFile.new(File.join(Rails.root, 'public', 'images', '128.jpg')) }
password 'password'
password_confirmation 'password'
end
end
support/controller_macros.rb
module ControllerMacros
def login_client
before do
#client = create(:client)
##request.env['devise.mapping'] = Devise.mappings[:client]
sign_in #client
end
end
end
FactoryGirl.define do
factory :contract do
sequence(:title) { |n| "translation#{n}" }
amount 150
additional_information 'X' * 500
due_date { 21.days.from_now }
association :user, factory: :client
association :user, factory: :contractor
end
end
I run test
rspec spec/controllers/contracts_controller_spec.rb
require 'rails_helper'
describe ContractsController do
login_client
let(:contract) { create(:contract) }
describe 'POST #create' do
context 'with valid attributes' do
it 'redirects to payment page' do
post :create, contract: attributes_for(:contract)
expect(response).to redirect_to payment_new_path
end
end
end
end
Error display:
Failure/Error: post :create, contract: attributes_for(:contract)
FactoryGirl::AttributeDefinitionError:
Attribute already defined: user
What is wrong in factory or test?
Factory :contract defines two attributes named user, which isn't allowed.
Give them unique (within the factory) labels, e.g.:
FactoryGirl.define do
factory :contract do
sequence(:title) { |n| "translation#{n}" }
amount 150
additional_information 'X' * 500
due_date { 21.days.from_now }
association :client, factory: :client
association :contractor, factory: :contractor
end
end
As they seem fitting, I've chosen attribute names corresponding with the factory names. This allows to even shorten this, by leaving out the factory name:
FactoryGirl.define do
factory :contract do
sequence(:title) { |n| "translation#{n}" }
amount 150
additional_information 'X' * 500
due_date { 21.days.from_now }
client
contractor
end
end
(See http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md, section "Associations":
If the factory name is the same as the association name, the factory name can be left out.
)
I have 2 classes Users and Authentications, then Authentications has_many Users:
User class:
FactoryGirl.define do
factory :user do
first_name "Juan"
last_name "Iturralde"
sequence(:email) { |n| "person-#{n}#example.org" }
password "1234567890"
password_confirmation "1234567890"
is_admin false
factory :admin do
is_admin true
end
after(:create) do |user|
create(:authentication, user: user)
end
end
end
Authentication class:
FactoryGirl.define do
factory :authentication do
user {User.first || create(:user)}
provider "Apple"
uid "uid"
end
end
And i dont now. How create an user in authentication?
To build associations in specs I use the following:
# spec/factories/posts.rb
FactoryGirl.define do
factory :post do
title 'The best post'
trait :with_comments do
create_list :comment, 3
end
end
end
# spec/factories/comments.rb
FactoryGirl.define do
factory :comment do
post
content 'Really awesome post'
end
end
# in specs
. . .
let(:post_with_commments) { create :post, :with_comments }
. . .
There are 2 factories (using FactoryGirl):
FactoryGirl.define do
factory :owner do
name 'Some name'
email 'test#gmail.com'
phone '89020000000'
password '123456'
password_confirmation '123456'
role
end
end
And roles:
FactoryGirl.define do
factory :role do
description 'Owner'
end
factory :superuser_role do
description 'Superuser'
end
end
I want to add a :superuser factory that has the same fields as :owner except role. How can I do it? Thanks.
I think this will work
FactoryGirl.define do
factory :owner do
name 'Some name'
email 'test#gmail.com'
phone '89020000000'
password '123456'
password_confirmation '123456'
role
factory :superuser do
association :role, factory: :superuser_role
end
end
end
I am using FactoryGirl in my rails application. I have following code snippet
FactoryGirl.define do
factory :pub_company, :class => Company do |c|
c.name "Dixons Group"
c.address "1500 Martin Ave"
c.city "Santa Clara"
c.state "CA"
end
end
FactoryGirl.buid(:pub_company)
Above code can creates a record fine. I am trying to create more data as similar to above one. so, i am using
require 'factory_girl_rails'
FactoryGirl.define do
factory :new do
FactoryGirl.create(:pub_company)
FactoryGirl.create(:adv_user)
FactoryGirl.create(:adv_setting)
FactoryGirl.create(:adv_ad)
end
end
it fails for me. Is this possible using factorygirl to call a different factory and create a record?
Since, the FactoryGirl is used mostly for testing models, I show the sample models, and factories, spec for the new model.
app/models/new.rb
class New < ActiveRecord::Base
attr_protected :company_id
belongs_to :company
end
app/models/company.rb
class Company < ActiveRecord::Base
attr_accessible :name, :address, :city, :state
has_many :news
end
spec/factories/company_factory.rb
FactoryGirl.define do
factory :company do |c|
c.name "Dixons Group"
c.address "1500 Martin Ave"
c.city "Santa Clara"
c.state "CA"
end
end
spec/factories/new_factory.rb
FactoryGirl.define do
factory :new do
company { FactoryGirl.create :company }
# adv_user { FactoryGirl.create :adv_user }
# adv_setting { FactoryGirl.create :adv_setting }
# adv_ad { FactoryGirl.create :adv_ad }
end
end
NOTE: You shell to have adv_user, adv_setting, adv_ad defined also.
spec/models/new_spec.rb
describe New do
it 'Test new model' do
new_instance = FactoryGirl.create :new
new_instance.pub_company.name.should == "Dixons Group"
...
end
end