factory-bot

Factory Girl - what's the purpose?

为君一笑 提交于 2019-11-30 10:20:07
问题 What's the purpose of Factory Girl in rspec tests when I could use before(:each) blocks? It feels like the only difference between Factory Girl and a before(:each) is that the factory prepares object creation outside of the test. Is this right? 回答1: Gems like Factory Girl and Sham allow you to create templates for valid and re-usable objects. They were created in response to fixtures which where fixed records that had to be loaded into the database. They allow more customization when you

rspec test passes in isolation, but fails when run with other tests

北战南征 提交于 2019-11-30 08:33:27
I have some specs, written in RSpec, that test various models. I use Factory Girl to generate object for testing. Now the most peculiar thing happens: When i run rspec spec/models/specific_model_spec.rb --- this passes all the tests in that spec However, when I run rspec spec/models --- every test in this spec fails referring to an invalid association being created (via a factory) The association created by the factory is obviously valid as running the test in isolation also shows. What could be causing this behavior? Update: The error i get when running the spec together with other specs (the

Factory Girl sequences not incrementing

泪湿孤枕 提交于 2019-11-30 07:27:25
问题 I'm trying to get FactoryGirl to generate some names for me, but the sequence doesn't seem to increment. # spec/factories/vessel.rb require 'factory_girl' FactoryGirl.define do sequence :vessel_name do |n| "TK42#{n}" end factory :vessel do name FactoryGirl.generate(:vessel_name) vessel_type 'fermenter' volume_scalar 100.0 volume_units 'bbl' end end # spec/models/vessel_spec.rb require 'spec_helper' describe Vessel do context 'working in the factory' do it 'makes a valid vessel' do vessel =

Upgrading to devise 3.1 => getting Reset password token is invalid

。_饼干妹妹 提交于 2019-11-30 07:02:43
问题 Solution Thanks to this gist form Steven Harman, I got it working. devise_mail_helpers.rb module Features module MailHelpers def last_email ActionMailer::Base.deliveries[0] end # Can be used like: # extract_token_from_email(:reset_password) def extract_token_from_email(token_name) mail_body = last_email.body.to_s mail_body[/#{token_name.to_s}_token=([^"]+)/, 1] end end end I added the file devise_mail_helpers.rb to the same folder as the features specs and wrote this spec. require 'devise

FactoryGirl build_stubbed strategy with a has_many association

此生再无相见时 提交于 2019-11-30 06:12:52
问题 Given a standard has_many relationship between two objects. For a simple example, let's go with: class Order < ActiveRecord::Base has_many :line_items end class LineItem < ActiveRecord::Base belongs_to :order end What I'd like to do is generate a stubbed order with a list of stubbed line items. FactoryGirl.define do factory :line_item do name 'An Item' quantity 1 end end FactoryGirl.define do factory :order do ignore do line_items_count 1 end after(:stub) do |order, evaluator| order.line

“Could not find a valid mapping for #<User …>” only on second and successive tests

蓝咒 提交于 2019-11-30 04:29:55
I'm trying to write a request test that asserts that the proper links appear on the application layout depending in whether a user is logged in or out. FWIW, I'm using Devise for the authentication piece. Here's my spec: require 'spec_helper' require 'devise/test_helpers' describe "Layout Links" do context "the home page" do context "session controls" do context "for an authenticated user" do before do # I know these should all operate in isolation, but I # want to make sure the user is explicitly logged out visit destroy_user_session_path @user = Factory(:user, :password => "Asd123",

Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT

隐身守侯 提交于 2019-11-30 02:41:52
问题 This is my Gemfile config: group :development, :test do gem 'rspec-rails' gem 'factory_girl', '~>2.0.0.beta1' gem 'factory_girl_rails', :git => 'https://github.com/thoughtbot/factory_girl_rails.git', :tag => 'v1.1.beta1' end This is my spec_helper.rb : # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require "factory_girl" # Requires supporting ruby files

Setting roles through rolify in FactoryGirl definition

浪尽此生 提交于 2019-11-30 01:28:17
I am using devise, rolify and cancan. I'm also using rspec with FactoryGirl for testing. Right now I'm working on some tests and I want to define users with different roles for those tests. Here is my current guess for how to do that using FactoryGirl: FactoryGirl.define do factory :user do name 'Test User' email 'example@example.com' password 'please' password_confirmation 'please' # required if the Devise Confirmable module is used confirmed_at Time.now factory :admin do self.has_role :admin end factory :curator do self.has_role :curator end factory :super_admin do self.has_role :super_admin

Why isn't factory_girl operating transactionally for me? - rows remain in database after tests

这一生的挚爱 提交于 2019-11-29 23:42:56
I'm trying to use factory_girl to create a "user" factory (with RSpec) however it doesn't seem to be operating transactionally and is apparently failing because of remnant data from previous tests in the test database. Factory.define :user do |user| user.name "Joe Blow" user.email "joe@blow.com" user.password 'password' user.password_confirmation 'password' end @user = Factory.create(:user) Running the first set of tests is fine: spec spec/ ... Finished in 2.758806 seconds 60 examples, 0 failures, 11 pending All good and as expected, however running the tests again: spec spec/ ... /Library

What's the difference between the build and create methods in FactoryGirl?

时光怂恿深爱的人放手 提交于 2019-11-29 20:47:48
The Factory Girl introduction delineates the difference between FactoryGirl.build() and FactoryGirl.create() : # Returns a User instance that's not saved user = FactoryGirl.build(:user) # Returns a saved User instance user = FactoryGirl.create(:user) I still doesn't understand the practical differences between the two. Can someone give an example where you would want to use one and not the other? Thanks! Helio Santos The create() method persists the instance of the model while the build() method keeps it only on memory. Personally, I use the create() method only when persistence is really