FAQ Database Discussion Community


Is it possible to override parts of an exisiting Spree factory?

factory-girl
I am building a Spree commerce application, and I'm using the factories provided by the gem in my specs. However, I find myself in the need to override some attributes for a specific factory to meet my application needs. Example: the User factory defined by Spree has 6 characters for...

Baffling error where FactoryGirl object has incorrect data, but only locally

ruby,debugging,rspec,factory-girl
This is baffling me and everyone else at the office. I have a factory girl insert like this: @data = FactoryGirl.create(:'database/table', :packagemember_tp => 3 ) puts 'kanga' puts @data.packagemember_tp puts 'kanga' Looks fine, but the log that prints out is this: kanga 2 kanga No matter what value I tell...

RSpec tests with Factory Girl creating image

ruby-on-rails,ruby,rspec,factory-girl,database-cleaner
In a rails 4.2.0 app I'm using RSpec and Factory Girl for tests. In the app I am using Paperclip for image uploads. When I run the tests with a blank image the image is being placed in the public folder. Is there any way I can use the database_cleaner...

Rails 4: Factory Girl & Rspec with associated Model

ruby-on-rails,ruby,rspec,tdd,factory-girl
I previously fixed an issue with some code that works though it is a little ugly. Problem now is that it breaks my tests! The idea here is that I can create a Campaign and associate 1 zip-file and one-to-many pdfs. Previous question and solution: Rails 4.2: Unknown Attribute or...

Failing RSpec tests of Controller with nested resource and associated FactoryGirl objects

ruby-on-rails,testing,rspec,tdd,factory-girl
I'm building a project to learn Rails and testing, and struggling to troubleshoot errors in an RSpec test of a controller that directs a nested resource. My code works as expected in a browser. I believe the problem relates to my test set-up and the associations of FactoryGirl objects. I...

How to reload a variable defined by let

ruby-on-rails,ruby,rspec,factory-girl
A user has many comments, so I would like to have a factory user with a comment associated to it (user_with_comment): factory :user, class: User do |t| ... factory :user_with_comment do |t| after(:create) do |user| FactoryGirl.create(:comment, user_id: user.id) end end It works fine... when I call FactoryGirl.create(:user_with_comment), it creates the...

Not require FactoryGirl before create

ruby-on-rails,factory-girl
I am trying out FactoryGirl and am using it in my seeds.rb to populate my database. I have to call FactoryGirl. before I call build or create. Is there a way I can not have to do that? Also, should I even be using FactoryGirl for seeding, or is it...

How to create DRYer Rspec with Factory Girl

ruby-on-rails,ruby,rspec,factory-girl,bdd
I am building a simple blog app in order to learn BDD/TDD with RSpec and Factory Girl. Through this process, I continue to run into 'Failures' but I believe they have more to do with how I am using Factory Girl than anything. As you'll see below, in order to...

How do you pass an attribute value to the has_many through join table with FactoryGirl?

ruby-on-rails,ruby,ruby-on-rails-4,rspec,factory-girl
food.rb factory: FactoryGirl.define do factory :food do factory :apple do description 'apple' sequence(:name) { |n| "apple #{n}"} long_description 'Apple' end factory :burger do description 'burger' sequence(:name) { |n| "burger #{n}"} long_description 'Burger' end after(:create) do |food| [:fat, :protein, :carb, :fiber].each do |nutrient| food.nutrients << FactoryGirl.create(nutrient, :measurement) end end end end...

Unit testing in rails using factory_girl

ruby-on-rails-3,unit-testing,factory-girl
I am trying to unit test a method inside a model as shown below in rails. The whole aim is to do a database independent testing here and so thinking of using Factory girl. For eg., Class A < ActiveRecord::Base def self.method_A(param1,param2) do /*do a select query on a table...

Rails / RSpec factory girl testing with invalid model produces strange behaviour

ruby-on-rails,ruby,rspec,factory-girl
I am testing my rails controller with rspec and factory_girl My actions returns error messages in json format with 412 status in case of invalid attributes format.json{render json: user.errors.messages, status: 412} like this When I am trying to test the functionality let(:invalid_attributes){ FactoryGirl.attributes_for(:user,password: "password") } it "returns error if invalid...

FactoryGirl - can't write unknown attribute, and undefined method `name'

ruby-on-rails,ruby,testing,associations,factory-girl
I'm trying to create a photo factory that's in a many to one association with gallery. And I'm getting two errors, depending on how I specify the association between those two models. Here's the models: Photo belongs_to :gallery, class_name: "SevenGallery::Gallery" Gallery has_many :photos, class_name: "SevenGallery::Photo", foreign_key: "seven_gallery_gallery_id", dependent: :destroy And...

Rspec::Matcher 'change' method with receiver and message vs. block

ruby-on-rails,activerecord,rspec,factory-girl
I'm trying to test the deletion of an association. The two models involved are User and Cancellation: class Cancellation < Active Record::Base belongs_to :taker, class_name: "User" end class User < ActiveRecord::Base has_many :taken_cancellations, class_name: "Cancellation", foreign_key: :taker_id end In my test, I've got the following code: describe "Admin Calendar" do...

Generate 3-letter strings with FactoryGirl/Faker

ruby-on-rails,ruby,factory-girl,faker
I would like to generate random, unique strings matching this pattern: [A-Z]{3}. How can I achieve this with FactoryGirl or Faker? I thought about FactoryGirl's sequences, but can't make it work....

Replace fixtures with factory girl in for generated scffold test cases

ruby-on-rails,rspec,factory-girl
I have generated a scaffold which auto generated specs for me,If I want to replace the default fixtures with factories of factory girl what is the procedure If I replace the Model.create! with FactoryGirl.build(:model) Test cases fails for GET #index assigns all tag_menus as @tag_menus DELETE #destroy destroys the requested...

Using Model Attributes inside another Model's Factory - Rails

ruby-on-rails,tdd,factory-girl,faker
I have a factory called Message, which HABTM Users. I want to mock sender_id inside the Message Factory. If I do: sender_id { FactoryGirl.build(:user}.id } I get: FactoryGirl:DuplicateDefinitionError How can I refer to another factory's data attribute (id in my case)? ...

How can I write factories for Models that validate each other?

ruby-on-rails,ruby-on-rails-4,rspec,factory-girl
Parent Class: class Parent < ActiveRecord::Base has_many :kids validates_presence_of :kids end Kid Class: class Kid < ActiveRecord::Base belongs_to :parent, inverse_of: :kids validates :parent, presence: true end Parent Factory: FactoryGirl.define do factory :parent do 2.times do parent.kids << FactoryGirl.create(:kid) end end end Kid Factory: FactoryGirl.define do factory :kid do parent end...

rspec + factorygirl : stack level too deep

ruby-on-rails,rspec,factory-girl
I have an Account and a User models. With Account: has_one :owner, class_name: 'User' and has_many :users. My first spec is failing : 1) User has a valid factory Failure/Error: Unable to find matching line from backtrace SystemStackError: stack level too deep # /usr/local/rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:222 I am looking for a loop,...

Factory Girl & Rspec controller test failure

ruby-on-rails,rspec,tdd,factory-girl,rspec3
I am still fairly new to testing and still wrapping my head around Factory Girl, which I believe to be the culprit of this failure. As simple as the solution will probably be, I have searched other posts with the same failure message but the answers are not working for...

Validation failed: Reviewer cannot write a review about yourself, Reviewer can't be blank, Reviewee can't be blank

ruby-on-rails,ruby,ruby-on-rails-3,rspec,factory-girl
I am writing controller tests for a Rails application where in users can write reviews about others (but not for themselves). reviews_controller_spec.rb describe "PATCH update" do it "redirects to user_reviews_url" do @user = FactoryGirl.create :user sign_in @user @user1 = FactoryGirl.create :user @review = FactoryGirl.create :review post :create, user_id:@user1.id, review:@review patch...

Populate Rails PostgreSQL hstore using factory_girl

ruby-on-rails,postgresql,factory-girl
I'm using Rails 4.2 and factory_girl to populate my test database. I have a model that uses an hstore for some unstructured data. How do I populate the hstore from factory_girl? Is it as simple as passing it a hash? Initially I tried this: FactoryGirl.define do factory :thingy do unstructured_data...

RSPEC ERROR: expected: [2015-05-01 19:56:25 -0700] got: nil (compared using ==) Ruby on Rails

ruby-on-rails,testing,rspec,controller,factory-girl
I am testing with ruby on rails (using rspec) - testing my events controller (specifically a variable assignment) like so: Controller Code: class EventsController < ApplicationController def index start_at = Time.zone.at(params[:start].to_f) puts start_at end The puts start_at command above gives the output: 2015-05-01 19:56:25 -0700 RSPEC Test Code: describe EventsController...

How do I test these callbacks on 1 of my models?

ruby-on-rails,ruby-on-rails-4,rspec,factory-girl,shoulda
Please Note: This question may seem similar to my other question, and it is just that...similar. Not identical. I replicated some of the info because it is relevant to the question. That being said, I didn't want to overwhelm either question with too many requests, so I split them up....

Rspec: Factory Girl & Complex Associations

ruby-on-rails,ruby,rspec,tdd,factory-girl
I am trying to test a helper method that would return the amount of Subscribers a Campaign is targeting. Being fairly new to TDD, I am having a hard time wrapping my head around how to write this one since the associations are quite complex. A Campaign "targets" by whether...

ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint

ruby-on-rails,rspec,factory-girl
I've combined my user & shift factories to save space. FactoryGirl.define do factory :user do name 'test' password 'test' phone_number '1-444-555-8888' end factory :shift do user end end This is my test. Fails on 'shifts = create_list(:shift, 20)' require 'spec_helper' describe MyFirebase do let(:dummy_class) { Class.new { include MyFirebase }...

uninitialized constant User (NameError)

ruby-on-rails,ruby,gem,factory-girl,rspec3
I have a gem where I have moved the core parts of all the models from my main app into modules that are essentially mixins or "concerns". Thus allowing me to use those mixins from the gem in models of various applications and thus allowing me to replicate the tables...

Factory still not registered after trying solutions provided

ruby-on-rails,factory-girl,rspec-rails
I am trying to write an Rspec test for the first time, my model test worked out fine, but I have problems with my controller test. Which seems weird. For my model test, I'm following the example from: https://gist.github.com/kyletcarlson/6234923 I am given the infamous Factory not registered: error. The log...

FactoryGirl error: Factory not registered

ruby-on-rails-4,rspec,factory-girl
Just beginning to learn testing... literally beginning. Using rspec-rails, factory_girls_rails and fakers with Rails 4. I have an Account and a User models. With Account: has_one :owner, class_name: 'User' My first model test : ## /spec/models/account_spec.rb require 'rails_helper' RSpec.describe Account, :type => :model do it "has a valid factory" do...

RSpec controller tests of PUT update action on nested resource

ruby-on-rails,rspec,factory-girl
I'm learning Rails and how to test using RSpec. I need help completing an RSpec controller test of the PUT update action. The controller directs a nested resource. The nested route and related program code work as expected in a web browser. I only need help writing the RSpec test...

Rails testing non-AR model(class) with FactoryGirl

ruby-on-rails,ruby,factory-girl
I have a class that looks like this: class Killmail::Parser def initialize(body) @body = body end end I also have a factory for it: FactoryGirl.define do factory :parser, class: Killmail::Parser do skip_create body '2013.12.02 19:24 bla bla' initialize_with { new(attributes) } end end It all works fine as long as...

Why this factory does not work?

ruby-on-rails,factory-girl
I'm trying to unit test my app.I have a order model and this model has a attr_accessor register_client.If accessor has a value 1: order.client = User.create it works, but when I trying to test this - I create a factory FactoryGirl.define do factory :order do username Faker::Name.name register_client "1" end...

Difference between Factory_Girl linting and Rspec's expect to be_valid

ruby-on-rails,rspec,tdd,automated-tests,factory-girl
What is the difference between testing factories with [Factory_Girl Lint]1, and RSpec's: ... it 'has a valid factory' do expect(build(:foo)).to be_valid end ... Am I correct to assume that Factory_Girl Lint checking for schema / db level validations like null: false, and the Rspec's expect to be_valid checks for model-level...

How do I associate a single record with a multiple factory girl models?

ruby-on-rails,rspec,factory-girl
I have a User model and Country model. There are a limited set of countries (only 1 right now) and in my factory, I'm not sure how to associate each User record with a single Country. I tried creating a country factory and associating each user with a country but...

factory girl, association in default trait, but leave it unassociated when trait is passed

ruby-on-rails,rspec,factory-girl
I'm testing with rspec a Company model that has several Addresses. I'm using factory girl to produce my test environment, I would like the default Company to have a few addresses registered, while I would like to have a trait for a specific case of a Company without any Address....

Rspec and Rails NoMethodError for comparison operator

ruby-on-rails,ruby,rspec,factory-girl
I am testing a User model with the following spec: require 'rails_helper' require 'spec_helper' RSpec.describe User, type: :model do it "password length to be equal to or greater than 8 characters" do expect(FactoryGirl.build(:user, password: '12345678')).to be >= 8 end end The Factory looks like this: require 'faker' FactoryGirl.define do factory...

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

ruby-on-rails,factory-girl
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 │ │ ├──...

Factory Girl Nested Associations

ruby-on-rails,unit-testing,rspec,tdd,factory-girl
I have several Rails models with nested relationship using has_one through, and no matter how I seem to structure my FactoryGirl factories I can't get the relationship to be correctly setup. Models class User < ActiveRecord::Base has_one :subscription has_one :plan, through: :subscription has_one :usage_limit, through: :plan end class Subscription <...

rspec assigning variables - ERROR: expected: “Completed” got: “pending” (compared using ==)

ruby-on-rails,testing,rspec,controller,factory-girl
I am testing my controller in ruby on rails using RSPEC and Factories (using factory girl) Controller Code: class JobsController < ApplicationController before_action :authenticate_member! before_action :set_job def complete @job.status = "completed" @job.save! Event.where("job_id = ? AND starts_at > ?", @job.id, DateTime.now).destroy_all redirect_to scheduler_path puts @job.inspect end def set_job @job =...