rspec testing subclass sti methods rails 4 - ruby-on-rails

I am trying to build an rspec test for a method on an sti subclass and the test only reads the parent model's method. The method works in the app, just not in the rspec test. I can't figure out what I'm missing
models/animals/animal.rb
class Animal < ActiveRecord::Base
def favorite
"unicorn"
end
end
models/animals/mammal_animal.rb
class MammalAnimal < Animal
def favorite
"whale"
end
end
models/animals/cat_mammal_animal.rb
class CatMammalAnimal < MammalAnimal
def favorite
"tabby"
end
end
mammal_animal_spec.rb
require 'rails_helper'
RSpec.describe MammalAnimal, type: :model do
let(:cat_mammal_animal) {FactoryGirl.create(:cat_factory)}
subject(:model) { cat_mammal_animal }
let(:described_class){"MammalAnimal"}
describe "a Cat" do
it "should initialize successfully as an instance of the described class" do
expect(subject).to be_a_kind_of described_class
end
it "should have attribute type" do
expect(subject).to have_attribute :type
end
it "has a valid factory" do
expect(cat_mammal_animal).to be_valid
end
describe ".favorite " do
it 'shows the favorite Cat' do
expect(cat_mammal_animal.type).to eq("CatMammalAnimal")
expect(cat_mammal_animal.favorite).to include("tabby")
expect(cat_mammal_animal.favorite).not_to include("whale")
expect(cat_mammal_animal.favorite).not_to include("unicorn")
print cat_mammal_animal.favorite
end
end
end
end
error
Failures:
1) MammalAnimal.favorite and .favorite shows the favorite Cat
Failure/Error: expect(cat_mammal_animal.type).to include("tabby")
expected "unicorn" to include "tabby"
# ./spec/models/mammal_animal_spec.rb:82:in `block (3 levels) in <top (required)>'
UPDATE
animals.rb
FactoryGirl.define do
factory :animal do
type 'Animal'
name "dragon"
trait :mammal do
type 'MammalAnimal'
name "zebra"
end
trait :cat do
type 'CatMammalAnimal'
name "calico"
end
factory :mammal_factory, traits: [:mammal]
factory :cat_factory, traits: [:cat]
end
end
as per a suggestion, I added the below line to the test
expect(cat_mammal_animal.class.constantize).to eq(CatMammalAnimal)
and got this error
1) MammalAnimal.favorite and .favorite shows the favorite Cat
Failure/Error: expect(cat_animal_mammal.class.constantize).to eq(CatMammalAnimal)
NoMethodError:
undefined method `constantize' for #<Class:0x007f8ed4b8b0e0>
Did you mean? constants

I think instead of using trait to create objects of subclasses, you should have separate factories for those too.
factory :animal do
name 'dragon'
end
factory :mammal, class: MammalAnimal do
name 'zebra'
end
factory :cat, class: CatMammalAnimal do
name 'calico'
end
All of these can be defined in animals.rb
Then you can create your objects like
create(:animal)
create(:mammal)
create(:cat)

Related

uninitialized constant Wing

When I am trying to run test case for model using Rspec and Factory girl it is showing uninitialized constant Wing
code in factories/wing.rb
FactoryGirl.define do
factory :wing do
wing_name "Example Title"
is_deleted "0"
mg_school_id "1"
created_by "2013-06-02 02:28:12"
updated_by "2013-06-02 02:28:12"
end
end
code in model/mg_wing_spec.rb
require 'rails_helper'
RSpec.describe MgWing, type: :model do
it "has a valid factory" do
#hai=FactoryGirl.create(:wing)
end
end
Change filename factories/wing.rb to factories/mg_wing.rb
Your factory name should be the same in your model and rspec filename.
Here are a couple of tutorials if you want it:
https://medium.com/#JonoYeong/setting-up-rspec-and-factory-girl-8cf287801099
https://semaphoreci.com/community/tutorials/working-effectively-with-data-factories-using-factorygirl
You either need the factory name same as the model name or pass the class param and factorygirl will take care of it
Solution 1
FactoryGirl.define do
factory :mg_wing do
...
end
end
Solution 2
FactoryGirl.define do
factory :wing, class: MgWing do
...
end
end

FactoryGirl undefined method `create='

I'm basically getting an error when I'm trying to use a factory I created for a Post model in Ruby on Rails.
Here's the full error:
Failure/Error: post = create(:post)
NoMethodError:
undefined method `create=' for #<Post:0x007fbf1be6e510>
Did you mean? created_at=
# ./spec/models/post_spec.rb:6:in `block (2 levels) in <top (required)>'
Here's the file for the factories:
spec/factories/post.rb
FactoryGirl.define do
factory :post do
title "Hello"
content "Hello, my name is Jacob."
user create(:user)
user_id 1
end
end
spec/models/post_spec.rb
require 'rails_helper'
require 'spec_helper'
describe Post do
it "has a valid factory" do
post = create(:post)
expect(post).to(be_valid)
end
end
I do have a spec/support/factory_girl.rb file which includes the FactoryGirl::Syntax::Methods. This file is loaded by spec/rails_helper.rb.
Also, the create(:user) line works and I'm able to use the user factory in the rails console, but not the post factory.
Any help would be fantastic. Thank you!
In your post factory, you have the syntax wrong for defining an associated record. Try defining it like this:
FactoryGirl.define do
factory :post do
title "Hello"
content "Hello, my name is Jacob."
user
end
end
You just need to state that the post has a user, and you certainly shouldn't be setting them all to have a specific user_id ... since each post will create a new user unless told otherwise and you have no idea what user_id will be generated.

Using factory girl to create HABTM association

I've been trying now for hours to get factorygirl to create two factories - one for users, one for organizations.
But I don't seem to understand how I can reflect a 'has_and_belongs_to_many' relationship in factories, as soon as I try to create an organization and associate it with an admin user, I run into various error messages (depending on the approach I use).
My model seems to be working fine, my seed file populates the dev DB and all the associations are created.
Right now my files look like this:
user factory
FactoryGirl.define do
factory :user do
email 'example#example.com'
password 'password'
password_confirmation 'password'
after(:create) {|user| user.add_role(:user)}
factory :owner do
after(:create) {|user| user.add_role(:owner)}
end
factory :admin do
after(:create) {|user| user.add_role(:admin)}
end
factory :superadmin do
after(:create) {|user| user.add_role(:superadmin)}
end
end
end
Organization factory
FactoryGirl.define do
factory :organization do |f|
f.name "example"
f.website "www.aquarterit.com"
f.association :users, :factory => :admin
end
end
in my specs I test this:
describe Organization do
it "has a valid factory" do
FactoryGirl.create(:organization).should be_valid
end
it "is invalid without a name" do
FactoryGirl.build(:organization, name: nil).should_not be_valid
end
it "is associated with at least one admin user" do
FactoryGirl.create(:organization)
it { should have_and_belong_to_many(:users)}
end
end
all three tests are failing, here are the error message:
1) Organization has a valid factory
Failure/Error: FactoryGirl.create(:organization).should be_valid
NoMethodError:
undefined method `each' for #<User:0x007fadbefda688>
# ./spec/models/organization_spec.rb:7:in `block (2 levels) in <top (required)>'
2) Organization is invalid without a name
Failure/Error: FactoryGirl.build(:organization, name: nil).should_not be_valid
NoMethodError:
undefined method `each' for #<User:0x007fadc29406c0>
# ./spec/models/organization_spec.rb:11:in `block (2 levels) in <top (required)>'
3) Organization is associated with at least one admin user
Failure/Error: organization = FactoryGirl.create(:organization)
NoMethodError:
undefined method `each' for #<User:0x007fadc2a3bf20>
# ./spec/models/organization_spec.rb:15:in `block (2 levels) in <top (required)>'
Any help is as always very much appreciated!
Update
In theory the same thing that works for assigning roles to the user should work for assigning an admin to the organization. But if I change organizations.rb to
FactoryGirl.define do
factory :organization do
name "example"
website "www.aquarterit.com"
after(:create) {|organization| organization.add_user(:admin)}
end
end
I get following error (I do have gem shoulda installed):
1) Organization is associated with at least one admin user
Failure/Error: it { should have_and_belong_to_many(:users)}
NoMethodError:
undefined method `it' for #<RSpec::Core::ExampleGroup::Nested_1:0x007ff2395f9000>
# ./spec/models/organization_spec.rb:16:in `block (2 levels) in <top (required)>'
Looks like you are not assigning users correctly and not creating the :admin user properly. For this to work, you need to assign an array of users to organization.users. And, you need to populate that array with a User instance (this assumes you have a User factory named :admin).
factory :organization do
name "example"
website "www.aquarterit.com"
after(:create) {|organization| organization.users = [create(:admin)]}
end
I do it this way, questions and tests have a HABTM relationship so:
FactoryGirl.define do
factory :question do
question 'Some stupid question'
user nil
factory :question_with_test do
# factory_girl's dynamic attributes, ignore it and pass it to evaluator
transient do
test nil
end
after(:create) do |question, evaluator|
create(:question_tests, question: question, test: evaluator.test)
end
end
end
end
Now I can create a question with HABTM to the Test model:
let(:test) { FactoryGirl.create :test, user: user }
let(:question_test_1) { FactoryGirl.create :question_with_test, user: user, test: test }
The question_tests factory is very basic:
FactoryGirl.define do
factory :question_tests, class: 'QuestionTest' do
question
test
end
end

Factory will create one object with a role, but not two

I am using rolify with a User model and a Task model (Rails 4). One of the roles a user can have is "owner." I want to use Factory Girl to create a user object and assign it a role. Here is my factory:
FactoryGirl.define do
factory :task do
owner "Steve"
agency "an agency"
facility "a facility"
description "This task must absolutely be done"
due_date "2013-12-22 03:57:37"
completed_date "2013-12-22 03:57:37"
factory :task_with_owner do
ignore do
user_id nil
end
after(:create) do |task, user_id|
User.find(user_id).add_role :owner, task
end
end
end
end
This spec passes:
it 'is capable of creating a valid object with owner' do
#user = create(:user)
task = create(:task_with_owner, user_id: #user.id)
expect(#user.has_role? :owner, task).to be_true
end
This spec fails:
it 'is capable of creating two valid objects with an owner' do
#user = create(:user, username: 'janeblow')
task = create(:task_with_owner, user_id: #user.id)
expect(#user.has_role? :owner, task).to be_true
task = create(:task_with_owner, user_id: #user.id)
expect(#user.has_role? :owner, task).to be_true
end
The error is:
Failure/Error: task = create(:task_with_owner, user_id: #user.id)
ActiveRecord::RecordNotFound:
Couldn't find User with id=#<#<Class:0x000000050f5e10>:0x00000004c9ed08>
# ./spec/factories/tasks.rb:19:in `block (4 levels) in <top (required)>'
# ./spec/models/role_spec.rb:15:in `block (2 levels) in <top (required)>'
Why?
Your after(:create) block looks a little wrong. Try changing it to the following:
after(:create) do |task, vars|
User.find(vars.user_id).add_role :owner, task
end
Then re-run your failing spec.
Because you told the factory to ignore the user_id being passed in and to use nil instead, in your after(:create) block you have to access it from the passed in attributes (in the second block argument, vars in this case). You were almost there, but were passing the object factory_girl uses to hold the attributes rather than the attribute itself from within that object.
See the Transient Attributes section here for another example - https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

Rails & RSpec - Testing Concerns class methods

I have the following (simplified) Rails Concern:
module HasTerms
extend ActiveSupport::Concern
module ClassMethods
def optional_agreement
# Attributes
#----------------------------------------------------------------------------
attr_accessible :agrees_to_terms
end
def required_agreement
# Attributes
#----------------------------------------------------------------------------
attr_accessible :agrees_to_terms
# Validations
#----------------------------------------------------------------------------
validates :agrees_to_terms, :acceptance => true, :allow_nil => :false, :on => :create
end
end
end
I can't figure out a good way to test this module in RSpec however - if I just create a dummy class, I get active record errors when I try to check that the validations are working. Has anyone else faced this problem?
Check out RSpec shared examples.
This way you can write the following:
# spec/support/has_terms_tests.rb
shared_examples "has terms" do
# Your tests here
end
# spec/wherever/has_terms_spec.rb
module TestTemps
class HasTermsDouble
include ActiveModel::Validations
include HasTerms
end
end
describe HasTerms do
context "when included in a class" do
subject(:with_terms) { TestTemps::HasTermsDouble.new }
it_behaves_like "has terms"
end
end
# spec/model/contract_spec.rb
describe Contract do
it_behaves_like "has terms"
end
You could just test the module implicitly by leaving your tests in the classes that include this module. Alternatively, you can include other requisite modules in your dummy class. For instance, the validates methods in AR models are provided by ActiveModel::Validations. So, for your tests:
class DummyClass
include ActiveModel::Validations
include HasTerms
end
There may be other modules you need to bring in based on dependencies you implicitly rely on in your HasTerms module.
I was struggling with this myself and conjured up the following solution, which is much like rossta's idea but uses an anonymous class instead:
it 'validates terms' do
dummy_class = Class.new do
include ActiveModel::Validations
include HasTerms
attr_accessor :agrees_to_terms
def self.model_name
ActiveModel::Name.new(self, nil, "dummy")
end
end
dummy = dummy_class.new
dummy.should_not be_valid
end
Here is another example (using Factorygirl's "create" method" and shared_examples_for)
concern spec
#spec/support/concerns/commentable_spec
require 'spec_helper'
shared_examples_for 'commentable' do
let (:model) { create ( described_class.to_s.underscore ) }
let (:user) { create (:user) }
it 'has comments' do
expect { model.comments }.to_not raise_error
end
it 'comment method returns Comment object as association' do
model.comment(user, "description")
expect(model.comments.length).to eq(1)
end
it 'user can make multiple comments' do
model.comment(user, "description")
model.comment(user, "description")
expect(model.comments.length).to eq(2)
end
end
commentable concern
module Commentable
extend ActiveSupport::Concern
included do
has_many :comments, as: :commentable
end
def comment(user, description)
Comment.create(commentable_id: self.id,
commentable_type: self.class.name,
user_id: user.id,
description: description
)
end
end
and restraunt_spec may look something like this (I'm not Rspec guru so don't think that my way of writing specs is good - the most important thing is at the beginning):
require 'rails_helper'
RSpec.describe Restraunt, type: :model do
it_behaves_like 'commentable'
describe 'with valid data' do
let (:restraunt) { create(:restraunt) }
it 'has valid factory' do
expect(restraunt).to be_valid
end
it 'has many comments' do
expect { restraunt.comments }.to_not raise_error
end
end
describe 'with invalid data' do
it 'is invalid without a name' do
restraunt = build(:restraunt, name: nil)
restraunt.save
expect(restraunt.errors[:name].length).to eq(1)
end
it 'is invalid without description' do
restraunt = build(:restraunt, description: nil)
restraunt.save
expect(restraunt.errors[:description].length).to eq(1)
end
it 'is invalid without location' do
restraunt = build(:restraunt, location: nil)
restraunt.save
expect(restraunt.errors[:location].length).to eq(1)
end
it 'does not allow duplicated name' do
restraunt = create(:restraunt, name: 'test_name')
restraunt2 = build(:restraunt, name: 'test_name')
restraunt2.save
expect(restraunt2.errors[:name].length).to eq(1)
end
end
end
Building on Aaron K's excellent answer here, there are some nice tricks you can use with described_class that RSpec provides to make your methods ubiquitous and make factories work for you. Here's a snippet of a shared example I recently made for an application:
shared_examples 'token authenticatable' do
describe '.find_by_authentication_token' do
context 'valid token' do
it 'finds correct user' do
class_symbol = described_class.name.underscore
item = create(class_symbol, :authentication_token)
create(class_symbol, :authentication_token)
item_found = described_class.find_by_authentication_token(
item.authentication_token
)
expect(item_found).to eq item
end
end
context 'nil token' do
it 'returns nil' do
class_symbol = described_class.name.underscore
create(class_symbol)
item_found = described_class.find_by_authentication_token(nil)
expect(item_found).to be_nil
end
end
end
end

Resources