factory-bot

FactoryGirl and polymorphic associations

拜拜、爱过 提交于 2019-11-29 20:23:00
The design I have a User model that belongs to a profile through a polymorphic association. The reason I chose this design can be found here . To summarize, there are many users of the application that have really different profiles. class User < ActiveRecord::Base belongs_to :profile, :dependent => :destroy, :polymorphic => true end class Artist < ActiveRecord::Base has_one :user, :as => :profile end class Musician < ActiveRecord::Base has_one :user, :as => :profile end After choosing this design, I'm having a hard time coming up with good tests. Using FactoryGirl and RSpec, I'm not sure how

How do I use factories from FactoryGirl in rails console

耗尽温柔 提交于 2019-11-29 20:01:08
I am using rails console in the development environment and I want to use factories. How can I get access to them? I have tried require "FactoryGirl" which returns 1.9.3p393 :301 > require "FactoryGirl" LoadError: cannot load such file -- FactoryGirl muttonlamb To solve this problem ensure that the factory bot gem is specifed in your Gemfile similar to this group :development, :test do gem 'factory_bot_rails' end Then bundle install . This should make FactoryBot class available in the development console. Hope this helps. Alex Popov I do this the following way: Start the rails console in test

Factory Girl - what's the purpose?

天涯浪子 提交于 2019-11-29 19:51:06
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? 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 instantiate the objects and they aim to ensure that you have a valid object to work with. They can be used

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

社会主义新天地 提交于 2019-11-29 11:58:25
问题 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

how to define factories with a inheritance user model

大憨熊 提交于 2019-11-29 05:27:32
I got following problem: In my application i use inheritance to define my user model: class User include Mongoid::Document field :name... field :bla... end class CustomUser < User field :customuserfield... end How can i write factories to map this Class hirachie in my specs. And keep up writing with don´t repeat yourself. FactoryGirl.define do factory :user do name "name" bla "bla" factory :custom_user do customfield "customfield" end end end This doesn´t work for me because the class is also "User". With "User" i got a invalid error because the customfields are not defiend here. Is there a

Factory Girl sequences not incrementing

可紊 提交于 2019-11-29 04:03:29
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 = FactoryGirl.create(:vessel) vessel.should be_valid, "Invalid vessel #{vessel.valid? || vessel.errors

FactoryGirl association model trouble: “SystemStackError: stack level too deep”

假装没事ソ 提交于 2019-11-29 02:01:35
I am using Ruby on Rails 3.0.9, RSpec-rails 2 and FactoryGirl. I am trying to state a Factory association model but I am in trouble. I have a factories/user.rb file like the following: FactoryGirl.define do factory :user, :class => User do attribute_1 attribute_2 ... association :account, :factory => :users_account, :method => :build, :email => 'foo@bar.com' end end and a factories/users/account.rb file like the following: FactoryGirl.define do factory :users_account, :class => Users::Account do sequence(:email) {|n| "foo#{n}@bar.com" } ... end end The above example works as expected in my

“undefined method `env' for nil:NilClass” in 'setup_controller_for_warden' error when testing Devise using Rspec

孤人 提交于 2019-11-29 00:57:17
I'm trying to create a spec for a sign out flow by using factorygirl to create a user and then use Devise's sign_in method to authenticate the user, then use capybara to click the "Sign Out" link. I'm getting (what seems to me to be) a strange error when I run the spec: Failures: 1) Sign out flow successfully redirects to the welcome index (root) Failure/Error: Unable to find matching line from backtrace NoMethodError: undefined method `env' for nil:NilClass # /home/vagrant/.rvm/gems/ruby-2.0.0-p576/gems/devise-3.4.1/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden' Finished in 0

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

橙三吉。 提交于 2019-11-29 00:55:44
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_mail_helpers.rb' include Features include MailHelpers describe "PasswordResets" do it "emails user when

How can I share the factories that I have in a GEM and use it in other project?

拜拜、爱过 提交于 2019-11-28 23:24:59
I have a gem that includes some Factories. The gem looks something like: . ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── db ├── lib │ ├── models │ │ ├── users.rb ├── pkg ├── core.gemspec ├── spec │ ├── factories │ │ └── users.rb │ ├── fixtures │ ├── helpers │ ├── integration │ ├── spec_helper.rb │ ├── support│ │ │ └── unit │ └── users_spec.rb └── tasks Now i'm using the gem in another Ruby project (Grape) by adding something like gem 'core', git: 'https://url.git' . Now everything is working fine as I can use User model from Grape project. However I want to use the factories (