I'm using RSpec with FactoryGirl within a Ruby on Rails environment for testing.
I want to specify my factories as follows:
factory :user do
role # stub
factory :resident do
association :role, factory: :resident_role
end
factory :admin do
association :role, factory: :admin_role
end
end
And I'd like to do something like this in my spec:
require 'rails_helper'
RSpec.describe User, type: :model do
context "all users" do
# describe a user
# subject { build(:user) }
# it { is_expected.to be_something_or_do_something }
end
context "residents" do
# describe a resident
# subject { build(:resident) }
# it { is_expected.to be_something_or_do_something }
end
context "admins" do
# describe a admin
# subject { build(:admin) }
# it { is_expected.to be_something_or_do_something }
end
end
Can this be done by explicitly setting the subject? When I do, I keep getting duplicate roles errors.
If anyone has any advice or suggestion, it would be greatly appreciated!
But this causes the user_spec.rb to use the :user factory.
No, it does not. Assuming you configured FactoryGirl correctly, RSpec can use whatever factory you'd like "on demand" in any test file. Configuration-wise, in rails_helper.rb throw this in:
RSpec.configure do |config|
# ...
config.include FactoryGirl::Syntax::Methods
# ...
end
Then, in your spec file:
require 'rails_helper'
RSpec.describe User, type: :model do
context "all users" do
let(:user) { create(:user) }
it 'is a user' do
# Here `user` is going to be a user factory
expect(user.unit).not_to be_present
end
end
context "residents" do
let(:user) { create(:resident) }
it 'is a resident' do
# Here `user` is going to be a resident factory
expect(user.unit).to be_present
end
end
context "admins" do
let(:user) { create(:admin) }
it 'is an admin' do
# Here `user` is going to be an admin factory
expect(user.role).to be('admin_role')
end
end
end
In short, you can use create(<factory_name>) on any factory definition that exists in any one of these paths:
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb
Note that if you haven't placed the config.include FactoryGirl::Syntax::Methods inside your RSpec.configure, you can still create any factory, by doing FactoryGirl.create(<factory_name>) instead of create(<factory_name>).
I don't think you would want to stop them from auto loading, and I'm not actually sure what your use case is for not allowing them to load?
RSpec automagically fetches the factory for a spec
Rspec loads all the factories into memory when your spec helper loads I believe. Because your using factory inheritence your just loading each of these into memory before your tests run, nothing is being called, no objects are being created or built. They are just ready to use in your tests.
Are you getting a specific error or is there some case I'm not seeing that you need?
I found the solution to my problems here: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations
What I needed to use in my user factories was association :role, factory: :role, strategy: :build
Related
So this is my first time writing unit tests, and Im incorporating Rspec w/FactoryBot.
My Specs were working just fine with using # instance variables, However when I use let! the second model fails because the first model was never created.
Spec:
require "rails_helper"
RSpec.describe Note, :type => :model do
before(:all) do
let!(:event){ FactoryBot.create(:event) }
let!(:note){ FactoryBot.create(:note) }
end
it "is valid with valid attributes" do
expect(note).to be_valid
end
end
Factories:
FactoryBot.define do
factory :note do
event_id Event.first.id
content "This is a sample note"
end
end
FactoryBot.define do
factory :event do
title "Event Factory Test"
event_date Date.today
event_time "1:30 PM"
end
end
As you can see, the note requires a Event id (Which requires the event to be created), but it complains when trying to find Event.first.id which should have been created from the let!).
Any ideas? This "seems" to be similar to how others use let in other rspec tests.
let and let! do not work if you wrap them in a before block.
require "rails_helper"
RSpec.describe Note, :type => :model do
let!(:event){ FactoryBot.create(:event) }
let!(:note){ FactoryBot.create(:note) }
it "is valid with valid attributes" do
expect(note).to be_valid
end
end
Also to setup an association within a factory simply pass the name of the factory:
FactoryBot.define do
factory :note do
event # short for association :event
content "This is a sample note"
end
end
(If the factory name is the same as the association name, the factory name can be left out.).
You're still thinking about factories wrong though. They should be factories that produce unique testable records. Not a set of fixtures. The way you have defined it the factory would only work if a event has been created. Never hardwire factories!
If you want to get the event later just do:
require "rails_helper"
RSpec.describe Note, :type => :model do
let!(:note){ FactoryBot.create(:note) }
it "has an event" do
expect(note.event).to be_a Event
end
end
I am running into a problem where a custom validation on my model is causing all of the shoulda validations to fail.
Essentially:
class User < ActiveRecord::Base
validates_presence_of :name
validate :some_date_validation
private
def some_date_validation
if date_given > birthday
errors.add(:birthday, "Some sort of error message")
end
end
end
And then in the spec:
require 'rails_helper'
RSpec.describe User, type: :model do
describe "shoulda validations" do
it { should validate_presence_of(:name) }
end
end
This will cause my test to fail because the other validation won't pass. Why is this?
You need to test using an instance of an object which is valid by default.
When you use the implicit subject in your Rspec test, Rspec will create a new instance of the object under test for you using the default initializer. In this case, User.new. This instance will be invalid because neither name is present nor is the custom validation going to pass.
If you are using factories (e.g. factory_girl) then you should create a User factory which sets all the attributes which make the validations pass.
FactoryGirl.define do
factory :user do
name "John Doe"
date_given Time.now
birthday 25.years.ago
end
end
Then use it in your tests
require 'rails_helper'
RSpec.describe User, type: :model do
describe "shoulda validations" do
subject { build(:user) }
it { should validate_presence_of(:name) }
end
end
You've now explicitly set the subject of your tests to be a new instance of User created by your factory. The attributes will be pre-set which means your instance is valid by default, and the tests should now be able to test each individual validation properly.
Every model I'm testing has the same "it must have atribute" test, testing the validates_presence_of for certain attributes. So, my goal is to create a "helper" that includes this test in a modular way.
Here is what I have:
# /spec/helpers.rb
module TestHelpers
# Runs generic validates_presence_of tests for models
def validate_presence( object, attributes=[] )
attributes.each do |attr|
it "must have a #{attr}" do
object.send("#{attr}=", nil)
expect(object).not_to be_valid
end
end
end
end
And
# /spec/rails_helper.rb
# Added
require 'helpers'
# Added
RSpec.configure do |config|
config.include TestHelpers
end
And
# /spec/models/business_spec.rb
require 'rails_helper'
RSpec.describe Business, type: :model do
describe "Validations" do
before :each do
#business = FactoryGirl.build(:business)
end
# Presence
validate_presence #business, %w(name primary_color secondary_color)
However, I'm getting the following error:
`validate_presence` is not available on an example group
I had read about shared_helpers and using it_behaves_as, but I'm not sure if that is what I'm looking for. Maybe I'm just thinking about doing this in the wrong manner.
--UPDATE--
If I place the validate_presence method into an it block, I get this error:
Failure/Error: it { validate_presence #business, %w(name primary_color secondary_color published) }
`it` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
Shared examples are for testing the same logic across different models. Here you're just testing one model so it wouldn't apply to you. Even though I don't recommend testing core validators such as presence, here is how you would do it
# /spec/models/business_spec.rb
require 'rails_helper'
RSpec.describe Business, type: :model do
let(:business) { FactoryGirl.build(:business) }
context "when validating" do
%w(name primary_color secondary_color).each |attribute|
it "checks the presence of #{attribute} value" do
business.send("#{attribute}=", nil)
expect(business).to_not be_valid
expect(business.errors[attribute]).to be_any
end
end
end
end
Also, the validate_presence helper you're trying to use is part of the shoulda-matchers library.
I'm writing basic tests for a simple CRUD rails application. I am using devise for authentication and Factory Girl for object creation whilst testing, after testing I am clearing the test.db with the Database Cleaner gem.
I am testing a controller with RSpec but need an admin user to be signed in for this to be a 'true' test.
So far I have been following documentation but I don't believe it is working.
I have a test which checks if the count has been changed by one:
describe "POST create" do
context "with valid attributes" do
it "creates a new room" do
expect{ post :create, room: FactoryGirl.attributes_for(:room) }.to change(Room,:count).by(1)
end
When I run the test suite I get the error:
expected #count to have changed by 1, but was changed by 0
From reading around it seems I need to setup authentication with my tests. To do this I have created relevant Factories:
# Devise User Class
factory :user do
email "basicuser#mvmanor.co.uk"
password "u"
password_confirmation "u"
admin false
customer false
end
# Admin
factory :admin, class: User do
email "basicadmin#mvmanor.co.uk"
password "a"
password_confirmation "a"
admin true
customer false
end
I have also created the relevant Devise mappings macros:
module UserControllerMacros
def login_admin
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryGirl.create(:admin) # Using factory girl as an example
end
end
def login_user
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in FactoryGirl.create(:user) # Using factory girl as an example
end
end
end
I'm not sure if I'm heading in the right direction with this. I certainly need my controller tests to be authenticated.
Before the first describe statement add this:
let!(:admin) { FactoryGirl.create(:admin) }
before { subject.stub(current_user: admin, authenticate_user!: true) }
it should stub your authentication.
And one little trick for your happiness: add in your spec_helper.rb anywhere within
RSpec.configure do |config|
...
end
block this code:
config.include FactoryGirl::Syntax::Methods
and now you don't need to preface all factory_girl methods with FactoryGirl, so instead of FactoryGirl.create you can write just create.
Here is my rspec file:
require 'spec_helper'
describe Classroom, focus: true do
describe "associations" do
it { should belong_to(:user) }
end
describe "validations" do
it { should validate_presence_of(:user) }
end
describe "instance methods" do
describe "archive!" do
before(:each) do
#classroom = build_stubbed(:classroom)
end
context "when a classroom is active" do
it "should mark classroom as inactive" do
#classroom.archive!
#classroom.active.should_be == false
end
end
end
end
end
Here is my Classroom Factory:
FactoryGirl.define do
factory :classroom do
name "Hello World"
active true
trait :archive do
active false
end
end
end
When the instance method test runs above, I receive the following error: stubbed models are not allowed to access the database
I understand why this is happening (but my lack of test knowledge/being a newb to testing) but can't figure out how to stub out the model so that it doesn't hit the database
Working Rspec Tests:
require 'spec_helper'
describe Classroom, focus: true do
let(:classroom) { build(:classroom) }
describe "associations" do
it { should belong_to(:user) }
end
describe "validations" do
it { should validate_presence_of(:user) }
end
describe "instance methods" do
describe "archive!" do
context "when a classroom is active" do
it "should mark classroom as inactive" do
classroom.archive!
classroom.active == false
end
end
end
end
end
Your archive! method is trying to save the model to the database. And since you created it as a stubbed model, it doesn't know how to do this. You have 2 possible solutions for this:
Change your method to archive, don't save it to the database, and call that method in your spec instead.
Don't use a stubbed model in your test.
Thoughtbot provides a good example of stubbing dependencies here. The subject under test (OrderProcessor) is a bona fide object, while the items passed through it are stubbed for efficiency.