factory-bot

FactoryGirl: Populate a has many relation preserving build strategy

試著忘記壹切 提交于 2019-12-03 00:03:10
My problem seems very common, but I haven't found any answer in the documentation or the internet itself. It might seem a clone of this question has_many while respecting build strategy in factory_girl but 2,5 years after that post factory_girl changed a lot. I have a model with a has_many relation called photos. I want to populate this has many relation preserving my choice of build strategy. If I call offering = FactoryGirl.build_stubbed :offering, :stay I expect offering.photos to be a collection of stubbed models. The only way i've found to achieve this is this one: factory :offering do

FactoryGirl + Faker - same data being generated for every object in db seed data

99封情书 提交于 2019-12-02 21:43:52
I am using FactoryGirl and Faker to generate user objects in my seeds.rb file but for some reason the exact same user is being created and rake db:seed is failing because of an email uniqueness validation. Factory for users: #users.rb require 'faker' FactoryGirl.define do factory :user do first_name Faker::Name.first_name last_name Faker::Name.last_name phone Faker::PhoneNumber.cell_phone email Faker::Internet.email password "password" password_confirmation "password" end end And the code in seeds.rb file: #seeds.rb rand(5..11).times { FactoryGirl.create(:user) } Error: ActiveRecord:

Rails 3.2, RSpec, Factory Girl : NameError: uninitialized constant Factory

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 21:41:43
Ive been following this introductory to Rails testing and Ive run into an issue I cant seem to find the solution to. Im very familiar with Rails but this is my first foray into testing. Anyhow, I have a very basic model test, not even fully implemented and when I try and run rspec spec/models/admin_spec.rb . I get the following error in the Admin has a valid factory line (full code below) Admin has a valid factory Failure/Error: Factory.create(:admin).should be_valid NameError: uninitialized constant Factory # ./spec/models/admin_spec.rb:6:in `block (2 levels) in <top (required)>' I assume

How to mock and stub active record before_create callback with factory_girl

只谈情不闲聊 提交于 2019-12-02 20:42:40
I have an ActiveRecord Model, PricePackage. That has a before_create call back. This call back uses a 3rd party API to make a remote connection. I am using factory girl and would like to stub out this api so that when new factories are built during testing the remote calls are not made. I am using Rspec for mocks and stubs. The problem i'm having is that the Rspec methods are not available within my factories.rb model: class PricePackage < ActiveRecord::Base has_many :users before_create :register_with_3rdparty attr_accessible :price, :price_in_dollars, :price_in_cents, :title def register

How to test model's callback method independently?

痞子三分冷 提交于 2019-12-02 20:11:45
I had a method in a model: class Article < ActiveRecord::Base def do_something end end I also had a unit test for this method: # spec/models/article_spec.rb describe "#do_something" do @article = FactoryGirl.create(:article) it "should work as expected" do @article.do_something expect(@article).to have_something end # ...several other examples for different cases end Everything was fine until I found it's better to move this method into a after_save callback: class Article < ActiveRecord::Base after_save :do_something def do_something end end Now all my tests about this method broken. I have

How to user factory girl to create associated lists with a has_many with a validation that requires it on create

微笑、不失礼 提交于 2019-12-02 19:13:31
In a Rails application, given three models User, Article and Reviewer with the following relationships and validations: class User < ActiveRecord::Base has_many :articles has_many :reviewers end class Reviewer < ActiveRecord::Base belongs_to :user belongs_to :article end class Article < ActiveRecord::Base belongs_to :user has_many :reviewers validate :has_reviewers? def has_reviewers? errors.add(:base, "article must have at least one reviewer.") if self.reviewers.blank? end end And the following factories using the newer DSL: FactoryGirl.define do factory :user do name { (8...20).map{ ('a'..'z

FactoryGirl + RSpec + Rails 3 'undefined method <attribute>='

爱⌒轻易说出口 提交于 2019-12-02 18:09:14
I'm fairly new to rails and TDD (as will no doubt be obvious from my post) and am having a hard time wrapping my brain around Rspec and FactoryGirl. I'm using Rails 3, rspec and factory girl: gem 'rails', '3.0.3' # ... gem 'rspec-rails', '~>2.4.0' gem 'factory_girl_rails' I have a user model that I've been successfully running tests on during development, but then needed to add an attribute to, called "source". It's for determining where the user record originally came from (local vs LDAP). In my factories.rb file, I have several factories defined, that look something like the following: # An

Factory-girl create that bypasses my model validation

怎甘沉沦 提交于 2019-12-02 16:28:26
I am using Factory Girl to create two instances in my model/unit test for a Group. I am testing the model to check that a call to .current returns only the 'current' groups according to the expiry attribute as per below... describe ".current" do let!(:current_group) { FactoryGirl.create(:group, :expiry => Time.now + 1.week) } let!(:expired_group) { FactoryGirl.create(:group, :expiry => Time.now - 3.days) } specify { Group.current.should == [current_group] } end My problem is that I've got validation in the model that checks a new group's expiry is after today's date. This raises the validation

How to create/build multiple instances of a factory in Factory Girl?

烈酒焚心 提交于 2019-12-02 16:17:57
How do I create multiple records or multiple factories of the same class? I tried: Factory.define :user do |user| user.email "someuser@somesite.com" user.password "somepassword" user.email "another_existing_user@somesite.com" user.password "somepassword" end and Factory.define :user do |user| user.email "someuser@somesite.com" user.password "somepassword" end Factory.define :user do |user| user.email "another_existing_user@somesite.com" user.password "somepassword" end But it doesn't work -- Attribute already defined: email . There are two steps to using Factories, the first is to define them,

RSpec failure: could not find table after migration…?

牧云@^-^@ 提交于 2019-12-02 15:11:58
I have a naked rails 3 app with one model, generated using rails g model User . I've added a factory (using factory_girl_rails ): Factory.define :user do |f| f.email "test@test.com" f.password "blah" f.password_confirmation "blah" f.display_name "neezer" end Then I've added one test: require 'spec_helper' describe User do subject { Factory :user } it "can be created from a factory" do subject.should_not be_nil subject.should be_kind_of User end end Then I migrate my database using rake db:migrate . Then I run the test using rspec spec , and the test fails with the following: Failures: 1) User