factory-bot

How do I create an association with a has_many :through relationship in Factory Girl?

跟風遠走 提交于 2019-12-05 04:50:15
问题 In my models I have the following setup: class User < ActiveRecord::Base has_many :assignments has_many :roles, :through => :assignments end class Role < ActiveRecord::Base has_many :assignments has_many :users, :through => :assignments end class Assignment < ActiveRecord::Base belongs_to :user belongs_to :role attr_accessible :role_id, :user_id end In my factory.rb file I have: FactoryGirl.define do factory :user do sequence(:username) { |n| "user#{n}" } email { "#{username}@example.com" }

Factory Girl error with has_many relationship

十年热恋 提交于 2019-12-05 04:11:42
I have the following factories: Factory.define :email do |email| email.email {"infomcburney.cowan.com"} end Factory.define :lead do |lead| lead.emails {|emails| [emails.association(:email)]} end Which are modeling the following classes class Lead < ActiveRecord::Base has_many :emails end class Email < ActiveRecord::Base belongs_to :lead, :class_name => "Lead", :foreign_key => "lead_id" end When I run the this test through shoulda: should "capture emails" do lead = Factory.build(:lead) assert_equal(1, lead.emails.size) end I get the following error: Factory::AttributeDefinitionError: Attribute

Factory Girl with polymorphic association for has_many and has_one

狂风中的少年 提交于 2019-12-05 03:55:49
问题 I am currently working on a project and I wanted to create tests using factory girl but I'm unable to make it work with polymorphic has_many association. I've tried many different possibilities mentioned in other articles but it still doesn't work. My model looks like this: class Restaurant < ActiveRecord::Base has_one :address, as: :addressable, dependent: :destroy has_many :contacts, as: :contactable, dependent: :destroy accepts_nested_attributes_for :contacts, allow_destroy: true accepts

Factory_girl has_one relation with validates_presence_of

a 夏天 提交于 2019-12-05 03:54:57
I have 2 Models: # user.rb class User < ActiveRecord::Base has_one :profile, :dependent => :destroy end # profile.rb class Profile < ActiveRecord::Base belongs_to :user validates_presence_of :user end # user_factory.rb Factory.define :user do |u| u.login "test" u.association :profile end I want to do this: @user = Factory(:user) => #<User id: 88,....> @user.profile => #<Profile id:123, user_id:88, ......> @user = Factory.build(:user) => #<User id: nil,....> @user.profile => #<Profile id:nil, user_id:nil, ......> But this doesn't work! It tells me that my profile model isn't correct because

I can't seem to add “config.include FactoryGirl::Syntax::Methods” to my rspec config block in spec_helper.rb

一笑奈何 提交于 2019-12-05 01:38:27
If I add: config.include FactoryGirl::Syntax::Methods under RSpec.configure do |config| and run rspec, I see this error: /Users/perry_mac/rails_projects/mymri/spec/spec_helper.rb:21:in `block in ': uninitialized constant FactoryGirl (NameError) my gemfile.lock can be seen in this pastebin my gemfile can be seen in this pastebin If I omit the Rspec.comfigure statment, my tests all run fine. I'd like to make use of the abbreviated syntax, but am not sure what I am doing wrong here. You must add this string in file 'spec/ RAILS _helper.rb' not in 'spec_helper.rb' Got it. This link showed me the

Rail 3.2.2/Devise: deprecation warning with rspec

删除回忆录丶 提交于 2019-12-05 01:03:05
I recently upgraded an app to rails 3.2.2. I'm using Factory_girl Factory.sequence :name do |n| "name-#{n}" end Factory.define :user do |u| u.first_name{ Factory.next(:name) } u.last_name { |u| 'last_' + u.first_name } u.password 'secret' u.password_confirmation { |u| u.password } u.sequence(:email) { |i| "user_#{i}@example.com" } end and this simple test specify { Factory.build(:user).should be_valid } generate the following warning DEPRECATION WARNING: You're trying to create an attribute user_id'. Writing arbitrary attributes on a model is deprecated. Please just use attr_writer` etc.

RSpec gives error 'trait not registered: name'

纵饮孤独 提交于 2019-12-05 00:01:03
I tried to test my Rails 3 application on Windows with RSpec. I've wrote tests and factories, but can't solve the issues which raise when I run RSpec on command line. Here is one of the test files: require 'spec_helper' describe "SignIns" do it "can sign in" do user = FactoryGirl.create(:user) visit new_user_session_path fill_in "login", with: user.username fill_in "password", with: user.password click_on "sign in" current_user.username.should == user.username end end And here's the factories.rb: factory :layout do name "layout1" end factory :club do sequence(:name) { |i| "Club #{i}" } contact

FactoryGirl: Factory not registered

拥有回忆 提交于 2019-12-04 23:02:49
I work on a project with a structure like: projects/warehouse/core projects/warehouse/products/bottles projects/warehouse/products/boxes In this project, the application logic, gems, etc. are all in the core application. I have boxes set up for rspec like such: projects/warehouse/products/boxes/spec /factories /models The factories directory contains cubics.rb : FactoryGirl.define do factory :cubic id 1 dimension 12 end end The models directory contains cubic_spec.rb : require 'spec_helper' describe Boxes::Cubic do it "has a valid factory" do FactoryGirl.create(:cubic).should be_valid end end

Rails 4, RSpec, MySQL2 default value error on first factory only

浪子不回头ぞ 提交于 2019-12-04 21:59:17
I'm upgrading from Rails 3.2.14 to Rails 4. When running RSpec on my test suite in my Rails 3 branch, all tests pass. On the Rails 4 branch, however, I'm getting an error on the first instance only of creating a factory. The error reads: ActiveRecord::StatementInvalid: Mysql2::Error: Field 'id' doesn't have a default value: INSERT INTO `of_of_measurements` (`aerobic_steps`, `calories`, `date`, `device_serial`, `distance`, `is_device_input`, `server_time`, `total_steps`, `user_id`) VALUES (15, 20, '2013-09-11 16:57:36', 'HJ1', 25.0, 0, '2013-09-11 16:57:36', 10, 1) My factory looks like this:

Rails/Devise - What should I test with devise and rspec?

ε祈祈猫儿з 提交于 2019-12-04 18:05:17
Many programmers use devise as their authentication solution and I would like to get their advice: Devise is already tested, but I want to know if there is something to test by myself (integration/unit/funtionnal tests?) for a standard devise integration with my knowledge (I'm not familiar with shoulda and cucumber, but I know a bit of rspec and factory girls) Thanks for your advices!! Testing the integration can be great. Because is how you integrate devise where you can do some error. Define some cucumber feature and it's OK. From the unit testing perspective, devise provides 2 helper