Questions tagged [factory-bot]
factory_bot is a Ruby gem that allows you to quickly define prototypes for each of your models and ask for instances with properties that are important to the test at hand.
1
vote
0answers
24 views
Rspec for OmniauthCallbacksController + Omniauth facebook + rails
I'm new to the rspecs. I request you to give some specific answer.As I tried many suggestions through my search. I'm unable to write the rspec for below scenario.
gemfile.rb
# frozen_string_literal:...
0
votes
0answers
10 views
how to test file size in Rspec
I'm new to Rspec. I'd like to test file size by the image that is over 10Mb in fixtures directory.
user_spec.rb
require 'carrierwave/test/matchers'
...
it "is invalid avatar size" do
image_path =...
0
votes
1answer
22 views
Why does FactoryBot keep assigning the same value for lat/lng each time?
I have the following locations factory:
FactoryBot.define do
factory :location do
valid_points = [
# a bunch of points in format ["lng", "lat"]
]
point = valid_points.sample
...
3
votes
2answers
57 views
Factorybot - How to set Nested attributes
I beleive it's better to create a new question... It follows my previous question my model product has many sizes (nested attributes)
I want to create Factories but I can't make it work...
A product ...
1
vote
1answer
48 views
Factory-bot - How to build association and nested attributes
I am new to Factories and I need help for the association and nested attributes....
How do I set an admin user that creates a product? OK
How do I set category to a product? Ok
How do I attach ...
1
vote
0answers
21 views
Evaluating context of factory for use as within an attribute value
I'm trying to DRY up a user factory that defines a couple sub classes:
FactoryGirl.define do
factory :user do
transient do
kind 'user'
end
sequence(:name) { |n| "Factory Girl #{...
1
vote
1answer
32 views
NoMethodError 'each' on FactoryBot has_many through association
I have a factory which generates a devise user w/ roles within an engine. The User model has_many :roles through: :roles_users. I can get the code to work with a after(:create) clause but not with the ...
0
votes
1answer
19 views
Rspec factory bot postgres UUID issue
I'm creating a user object using factory bot in an rspec feature spec.
The user belongs_to a company and has the below factory setup
FactoryBot.define do
factory :user do
company
...
0
votes
0answers
16 views
RuntimeError:Can't modify frozen hash rspec
I have a call RiskMatrix model that stores a score as a string
If I build the object using FactoryBot and then save I get :
RuntimeError:
Can't modify frozen hash
# /Users/mike/.rvm/gems/ruby-...
1
vote
1answer
18 views
FactoryGirl not aware of new column
Hi I am using FactoryGirl 4.9.0 w/ Rails 4.2.11. I added a boolean column to a model called Query like so:
class AddSetLatestResultToQueries < ActiveRecord::Migration
def change
add_column :...
0
votes
1answer
31 views
Rspec producing a undefined method `route_to' , despite having rspec/rails defined in gemfile/rails helper
QUESTION: What have I done wrong that the route_to method remains undefined?
I'm very new to this but I'm trying to develop some route tests via the rspec gem.
My issue is that I am obtaining the ...
0
votes
1answer
14 views
How to pass arguments, a parent class “B” id, when create() of factory is called from rspec, for a child class A?
I need to write a rspec test.I have child-parent instance variables who were created with the factory create method somehow. Now, I need to create another child, of the same parent.
i have called ...
0
votes
1answer
10 views
RSpec error: Factory not registered after migrate from FactoryGirl to FactoryBot
After migrate the factory FactoryGirl to FactoryBot, using the quick migration instructions, I got errors in my tests in RSpec regarding to factory registration.
ArgumentError:
Factory not ...
1
vote
1answer
26 views
How to spy just one call of ActiveSupport::Notifications #instrument, not all of them
I'm making a Rspec test that checks if ActiveSupport::Notification.instrument was called with some parameters.
The thing is that in order to make this test a need FactoryBot to build some objects, ...
0
votes
0answers
17 views
How do you create parent objects for a factory using before(:create) without sparking the parents' after_create callback?
I am trying to get this spec to pass:
it "queues up AbsenteeReservationEmailerJob" do
ActiveJob::Base.queue_adapter = :test
expect { subject }.to enqueue_job(AbsenteeReservationEmailerJob)
end
...
0
votes
2answers
139 views
Factory_bot can't create “project” factory
I am writing several factories within a project I am mantaining. All of them work except for the project factory.
When I run the factory in rails console I get the following error
FactoryBot.create(:...
0
votes
1answer
18 views
Factory Bot: Pass same object to association
Model has 2 associations. Each association belongs to the same "group". How do we pass the same group to each association?
factory :group_feature_config do
transient do
group
end
...
1
vote
3answers
30 views
Factory Bot HABTM nil value in join table
I am getting a (frustrating!) error when running rspec (with Factory Bot) tests for my Rails app. I'm seeing the following error:
ActiveRecord::NotNullViolation:
Mysql2::Error: Field '...
0
votes
2answers
45 views
FactoryBot factory with static attributes
I want to create an integration test for XML serialization, that retrieves data from database and later create the XML files.
I need to have some factories with static attributes, ie, they always have ...
0
votes
1answer
29 views
How to debug FactoryGirl.create callback errors in Rails?
First problem is that FactoryGirl shows the error message but don't say where occurs, file and line:
I have a byebug in a very simply spec file:
RSpec.describe Site::HomeHelper, type: :helper do
...
0
votes
1answer
30 views
How to test before action of ApplicationController in rails using RSpec
My ApplicationController looks like this
class ApplicationController < ActionController::Base
before_action :initialize_fields
protected
def initialize_fields
@company_id_super = nil
...
0
votes
1answer
17 views
“Ambiguous match, found 2 elements matching visible link” issue
I've looked through a few posts with the same issue, but still feel like mine is a bit different.
viewing_categories_spec.rb
require 'rails_helper'
RSpec.feature 'Users can view categories' do
...
0
votes
1answer
36 views
FactoryGirl: Accessing association data inside traits
I have a payment model, invoice model and a join table invoice_payments for allocating payments to invoices.
I am creating my payment factory like this:-
FactoryGirl.define do
factory :payment do
...
0
votes
0answers
21 views
How to make dependent factories in factorygirl
In my application. I have models - order has_many order_items, invoice has_many invoice_items. I am creating factory for order and order_items like this:-
Order Factory :-
FactoryGirl.define do
...
0
votes
1answer
46 views
FactoryBot sequence with parameter
I want to be able to increment different codes in the same manner. For example:
If the code is 1234: 1234, 1234A, 1234B, ...
If the code is 5678: 5678, 5678A, 5678B, ...
It would be nice if I could ...
0
votes
0answers
50 views
How to association model with self , I get this error ( FactoryBot::AssociationDefinitionError: Self-referencing association)
How to association Model self.
When I create model by:
let(:user) {create(:user)}
I tried with this follow codes, but get this error:
FactoryBot::AssociationDefinitionError:
Self-...
1
vote
1answer
35 views
Rspec before(:each) works but before(:all) does not
My ProductCategory spec:-
require 'rails_helper'
RSpec.describe ProductCategory, type: :model do
before(:each) do
@product_category = create(:product_category)
end
context "...
0
votes
1answer
43 views
Where should I create my shared FactoryBot associations?
I have a User model that has_one required Company. Using FactoryBot, I want to create several users that share that same company. I saw some advice which I interpreted as suggesting I create Company ...
0
votes
0answers
63 views
Factory Bot and Rspec with nested attributes and two foreing key to same table
I have two models with has_many association, the model User and UserRelation. My UserRelation has two foreign keys to User.
class UserRelation < ApplicationRecord
belongs_to :user
...
1
vote
2answers
50 views
RSpec: invalid model with searchkick
I'm not very sure I'm doing it right, but I have two models: House that has_one Address.
The Address model has:
class Address < ApplicationRecord
searchkick
belongs_to :house
end
I'm trying ...
0
votes
0answers
27 views
rspec expectations change depending on other expectations existing
I have a rails app tested with rspec using factorybot.
Users have many entries. Entries belong to a category.
Users have many categories through entries.
I created a user and put an expectation in to ...
0
votes
2answers
34 views
query about create has_many & belongs_to records using FactoryBot
I am writing Rspec requests spec, and before it I want to build some test data using FactoryBot.
And now I have a model Game:
class Game < ApplicationRecord
has_many :game_levels
and a model ...
0
votes
1answer
67 views
factory girl with faker generate same data in node express
I am trying to have factory girl generate test data:
export const adminDef = (factory: Static) =>
factory.define('User', User, {
email: internet.email(),
password: internet.password(),
...
2
votes
1answer
47 views
Validation failed: Logo can't be blank when logo uploaded using rspec
I am creating automated tests for my rails app using rspec and factoryBot. In my case I am creating a company with a requirement of having a logo. Creating a company in the test is not a problem but ...
0
votes
2answers
38 views
Factory fails after changing name
I have a factory that work's fine when its symbol is :notification_event but when I change the name to player_notification_event it fails with the error
uninitialized constant ...
0
votes
1answer
44 views
confirmation link in Rails test environment renders: Forbidden You don't have permission to access /users/confirmation on this server
I have a User class that uses devise :confirmable, which, when a user is created, triggers an email to be sent containing a confirmation link. This works perfectly in development and production, but ...
0
votes
1answer
36 views
Unable to build has_and_belongs_to_many relation in Rspec with FactoryBot
I read the documentation of FactoryBot here: https://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md
I have users and roles and it is a has_and_belongs_to_many relation. I have tried many ...
0
votes
1answer
53 views
FactoryBot how to create factories for inter related models
I am trying to write RSpec tests for legacy ROR app. I have following models
class Domain < ActiveRecord::Base
belongs to :account
...
end
class Account < ActiveRecord::Base
belongs to :...
0
votes
1answer
20 views
Running Multiple Factories of the Same Object in Production
At work, we're using Ruby 2.3.4, Rails 4.2.7, and FactoryGirl 4.0.0. We've run into the problem of having way too many traits in our base factories. We've started a project for replacing them and have ...
0
votes
1answer
18 views
Rails 3.2, SQL NoMethodError: undefined method `auth_group='
Full Error Message
1) AuthGroupResourceUser Validations has a valid factory
Failure/Error: expect(FactoryGirl.build(:auth_group_resource_user)).to be_valid
NoMethodError:
undefined method `...
-1
votes
1answer
43 views
Can't access non-persisted associations in RSpec
Problem: I have two models called Contract and Appendix. The latter has the scope persisted for excluding non-persisted objects. I'd like to write spec for the scope but seems I can't access non-...
0
votes
2answers
83 views
FactoryBot is not creating the associated models list in the after create callback
I have two factories as follows:
FactoryBot.define do
factory :proofread_document do
factory :proofread_document_with_paragraphs do
after(:create) {|instance| create_list(:paragraph, 5, ...
0
votes
1answer
37 views
Ruby gem Factory Bot:
My goal create factory with a few fields with the same value,
for example I want that name and full_name be equal.
FactoryBot.define do
factory :brand do
n = Faker::Company.name
name { ...
0
votes
0answers
37 views
FactoryBot - Define a factory that provides valid defaults for different overrides
A Sale belongs to an Item and a Price. An Item belongs to an Item_Type, and a Price also belongs to an ItemType. For any Sale s, we should have s.item.item_type == s.price.item_type, else the ...
1
vote
1answer
36 views
Pessimistic locking is not working properly with reload
Pessimistic locking is not working properly with reload (used in the test cases). with_lock and reload is not working correctly together and test cases are getting failed. If I removed the with_lock ...
1
vote
1answer
94 views
undefined method create for RSpec rails 5
This may a possible duplicate however it did not work for me.
Please let me know what did I miss.
Config details:
#Gemfile
gem 'factory_bot_rails' # 4.8.2
#spec/rails_helper.rb
RSpec.configure do |...
0
votes
1answer
56 views
Factory Bot associations with namespaced model glitching
When trying to build namespaced associations, Factorybot does not register the namespace, but rather uses its own despite defining it just like documentation (and every thread on SO says).
Model ...
0
votes
0answers
64 views
FactoryGirl / Bot and associations
I have two factories, a creative and an ad_source_creative with the latter being a lookup table.
If I run:
creative = FactoryGirl.create(:creative)
I get an error
Mysql2::Error: Column '...
3
votes
3answers
113 views
How do I add extra data to join table in factory_bot factories
I have models for Users and Sites with a has_many through relaitonship between them. There is one extra piece of data on the sites_users join table is_default which is type boolean with the intention ...
0
votes
2answers
78 views
Factory bot for type column in rails model
I have a model name collaborator.
Collaborator fields are:(id, type, created_at, updated_at)
Also I have two more model Manager and Guest.
Both Manager and Guest extends the Collaborator model. So ...