factory-bot

How to define factory girl in rails for model that isn't in root of the models folder?

好久不见. 提交于 2021-02-07 20:23:43
问题 I would like to create a factory girl for a model in my server which is inside a folder in the models folder. My tree view looks like: ├── app | ├── models │ │ ├── xxx │ │ | ├── user.rb ├── spec │ ├── factories │ │ ├── xxx │ │ | ├── user.rb My factory girl looks like: FactoryGirl.define do factory :user do username { 'aaa' } end end When I try to build user I get the error: undefined method `new' for User:Module 回答1: Is your model defined under a namespace? For example, if your app/models/xxx

How can I add a function to FactoryBot globally?

落爺英雄遲暮 提交于 2021-01-28 11:00:51
问题 I have a class that extends FactoryBot to include functionality to copy Rails' .first_or_create . module FactoryBotFirstOrCreate def first(type, args) klass = type.to_s.camelize.constantize conditions = args.first.is_a?(Symbol) ? args[1] : args[0] if !conditions.empty? && conditions.is_a?(Hash) klass.where(conditions).first end end def first_or_create(type, *args) first(type, args) || create(type, *args) end def first_or_build(type, *args) first(type, args) || build(type, *args) end end I can

Ruby Gem: Uninitialized constant FactoryBot

浪子不回头ぞ 提交于 2020-08-22 07:35:30
问题 Working on a Ruby gem and trying to use FactoryBot inside with RSpec. I have this in support/factory_bot.rb : RSpec.configure do |config| config.include FactoryBot::Syntax::Methods config.before(:suite) do FactoryBot.find_definitions end end and in spec_helper.rb : require 'support/factory_bot' When I try to run the spec rake task, I get this error: support/factory_bot.rb:2:in `block in <top (required)>': uninitialized constant FactoryBot (NameError) What am I missing? This used to work fine

Ruby Gem: Uninitialized constant FactoryBot

不问归期 提交于 2020-08-22 07:35:06
问题 Working on a Ruby gem and trying to use FactoryBot inside with RSpec. I have this in support/factory_bot.rb : RSpec.configure do |config| config.include FactoryBot::Syntax::Methods config.before(:suite) do FactoryBot.find_definitions end end and in spec_helper.rb : require 'support/factory_bot' When I try to run the spec rake task, I get this error: support/factory_bot.rb:2:in `block in <top (required)>': uninitialized constant FactoryBot (NameError) What am I missing? This used to work fine

trouble building with factory girl

我怕爱的太早我们不能终老 提交于 2020-02-07 05:28:22
问题 so the issue is that mail is not being added to ActionMailer::Base.deliveries. FactoryGirl.create is returning nil. what am I doing wrong? FactoryGirl.define do factory :provisional_user do sequence(:email) { |n| "bangbang_#{n}@example.com" } first_name "Provisional" last_name "User" partner "source2" unsubscribed false end factory(:unsubscribed_user, :parent => :provisional_user, :class => ProvisionalUser) do sequence(:email) { |n| "do_not_contact@example.com" } first_name "Unsubscribed"

what's the practical difference between these two FactoryGirl declarations

自作多情 提交于 2020-01-25 08:42:27
问题 I figured out this bug that took me way too long to track down. I had this: FactoryGirl.define do factory :global_list do list_id FactoryGirl.create(:user).liked_items_list.id end end but just wrapped in a block: FactoryGirl.define do factory :global_list do list_id { FactoryGirl.create(:user).liked_items_list.id } end end So I know that in the second call, the block causes it to not run until an actual call like FactoryGirl.create(:global_list) is made; I assume this is getting passed and

Factory Girl has_many through validation deadlock

江枫思渺然 提交于 2020-01-25 03:03:26
问题 I have a Listing model which has_many :categories, through: :categories_listings and has_many :categories_listings . I was testing it with a Factory Girl factory which looks like this: factory :listing do |f| f.sequence (:title) { |n| "Listing #{n}" } message {Faker::Lorem.paragraph} cards {[FactoryGirl.create(:card)]} categories {[FactoryGirl.create(:category)]} association :delivery_condition association :unit_of_measure quantity 1 unit_price 1 end And everything was OK until I added the

FactoryGirl creates user, but save point is released before test starts

瘦欲@ 提交于 2020-01-23 01:55:11
问题 I am running rspec tests for spec/requests/user_pages_specs: require 'spec_helper' describe "User pages" do subject { page } describe "home page" do before { visit root_path } it { should have_content('Sign up with Facebook') } it { should have_title(full_title('')) } end describe "profile page" do let(:user) { FactoryGirl.create(:user) } before { visit user_path(user.id) } it { should have_content(user.name) } it { should have_title(user.name) } end end As you can see, I am using FactoryGirl