All Questions
26 questions
0
votes
1
answer
68
views
How to prevent ActiveRecord from making an associated record in a callback before it is saved?
I have two models, Branch and Organization. An organization has many branches. A branch cannot exist without an associated organization, and an organization cannot exist without at least one branch. ...
-1
votes
1
answer
101
views
build record with has_many through association, no db
In the context of testing a method not querying the db when given a preloaded relation, I would like to build a record with preloaded associations. I run into trouble when trying to build a record ...
0
votes
1
answer
633
views
rails factory rspec tests failing because of "only_integer" validation in model
I have a factory for my model 'price' but all rspec tests start failing when I put the validation for price as only_integer. The error i get when i try to validate factory using rspec is "Price must ...
0
votes
0
answers
698
views
FactoryGirl build has_many through associations
Is it possible to build associations that have a has_many through association in ActiveRecord?
Example: I have a user that has team_memberships associated with it, and those team_memberships have a ...
1
vote
2
answers
1k
views
FactoryGirl cannot assign to a enum validated parameter
I am using RSpec, FactoryGirl and ActiveRecord. The following test is not passing:
require 'spec_helper'
describe User, type: :model do
it "has a valid factory" do
expect(FactoryGirl....
0
votes
1
answer
122
views
Failing Rails 4 controller test - ActiveRecord::AssociationTypeMismatch?
I'm building a Rails (4.1.8) application with Postgres (0.18.3), Rspec (3.1.0), and FactoryGirl (4.5.0). I need help troubleshooting a controller test, which is throwing an Active Record::...
3
votes
1
answer
2k
views
Uniqueness field using Scope of association
I want to insure that an items name is unique within an organization. So I've used the "validates_uniqueness_of :name, scope: [:organization]" within Item. This unfortunetely didn't work.
Error (...
0
votes
2
answers
91
views
ActiveRecord association not being set in the database?
I’m sure this is a “duh" newbie-type question, but I’ve been at it for days and cannot figure out why my code isn’t setting a relationship in the database correctly. I have a simple belongs_to ...
1
vote
1
answer
55
views
Rspec Controller Testing - Errror: Expected [#<Contact id: 412...] Got [#<Contact id: 413]
I am using Ruby on Rails, RSPEC, and Factory Girl to write a test for my post action in my contact controller: here is my test:
describe "POST CREATE" do
it "assigns @contact and redirects" do
...
0
votes
1
answer
749
views
RSPEC Error: Expected #<ActiveRecord::Associations::CollectionProxy > got: nil
I am trying to test the following controller action(Using Ruby on Rails, RSPEC, and FactoryGirl):
class ContactsController < ApplicationController
before_action :authenticate_user!
def index
...
1
vote
1
answer
410
views
Rspec FactoryGirl with has_many through got MissingAttributeError
I have has_many through relations:
Poll that has_many votee (User) through History:
has_many :polls_through_history, through: :history, class_name: "Poll", foreign_key: "poll_id", source: :poll
and
...
0
votes
2
answers
982
views
Factory Girl - Make record and call function on it
Whenever I make a User, I want to call build_user_group on that user. I use after_save:build_user_group on the User model to do this, but when FactoryGirl creates a user, build_user_group is never ...
2
votes
1
answer
603
views
Rspec::Matcher 'change' method with receiver and message vs. block
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"
...
0
votes
1
answer
437
views
FactoryGirl loses belongs_to built in Model.before_create
Given this model:
class User < ActiveRecord::Base
belongs_to :user_profile
before_create { build_user_profile }
end
and this factory:
FactoryGirl.define do
factory :user do
end
end
when ...
3
votes
2
answers
2k
views
Discover errors in Invalid Record factory girl
I have a user factory like this:
FactoryGirl.define do
factory :user do
email "[email protected]"
password "123456789"
end
end
I am trying to create a spec for the user:
describe User do
it ...
4
votes
2
answers
4k
views
RSpec with Factory_girl - destroy object
I wan't to test a relationship between two Models.
A course has many enrollments, an enrollment has one course.
When a course is being destroyed, all enrolments connected to it are being set to ...
0
votes
3
answers
1k
views
RSpec doesn't compare objects properly
I have the following spec:
require 'spec_helper'
describe Page do
it "has a valid factory" do
create(:page).should be_valid
end
it "is invalid without a title" do
build(:page, title: ...
2
votes
1
answer
214
views
ActiveRecordRelation where method matching on unique id returning object slightly diff then when traversing the array and matching on the same id
Pretty new to ruby, rails and unit testing so any additional feedback welcome!
I'm writing an rspec unit test for an ActiveRecord based model (contact) called #assign_campaign. ContactCampaign is an ...
4
votes
2
answers
1k
views
FactoryGirl association Parent can't be blank
I am trying to use FactoryGirl to create a test database with an association that is Parent has_many Entries. At this point, it is throwing the ActiveRecord validation error that Parent can't be ...
42
votes
6
answers
37k
views
How to test model's callback method independently?
I had a method in a model:
class Article < ActiveRecord::Base
def do_something
end
end
I also had a unit test for this method:
# spec/models/article_spec.rb
describe "#do_something" do
@...
0
votes
2
answers
745
views
Factory Girl Confirmation Validation in Model Spec
I am having trouble with Factory Girl when trying to test if email confirmation is nil.
Here is my model spec (user_spec.rb)
require 'spec_helper'
describe User do
it "is invalid without an ...
0
votes
1
answer
1k
views
RSpec - How to test a database view?
I have a class that is backing a database view. I am using FactoryGirl to try and test this so I go through the normal steps of creating a user instance like so:
before(:each) do
@user = ...
1
vote
1
answer
200
views
Alternatives to making _id mass assignable
I've got a spec for a rails controller, that tests creation of a associated model:
Models:
class Foo < ActiveRecord::Base
has_many :bars
end
class Bar < ActiveRecord::Base
belongs_to :foo
...
12
votes
3
answers
6k
views
Dependent factories in Factory Girl
I have 2 factories. Beta_user and Beta_invite. Basically before a Beta_user can validly save I have to create an entry of Beta_invite. Unfortunately these models don't have clean associations, but ...
4
votes
1
answer
3k
views
Strange problem with factory girl
I have a model
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# name :string(255)
# title :string(255)
# content ...
3
votes
1
answer
2k
views
Non-rails application with RSpec, ActiveRecord and Factory Girl
How to properly implement RSpec, ActiveRecord and Factory Girl in non-rails application. I also need that every test case would run on a clean database.
Thanks!