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
Related
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
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
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.
I have a :fakeUser factory defined in factories/user.rb, which I use in user_spec.rb correctly.
I want to be able to do the same, create(:fakeUser), in another rspec file potato_spec.rb.
How can I do this?
require 'spec_helper'
feature "your potato test" do
let!(:fakeUser ) { FactoryGirl.create(:fakeUser ) }
context "your test context" do
#...the actions you want to test
end
end
Within the context you may have different scenarios, for example "when user is logged in" or "when user is guest".
Provided that you have a factory users.rb like this:
FactoryGirl.define do
factory :fakeUser do
name 'John'
#... other attributes you might have for the user
end
end
I have started my journey with TDD in Rails and have run into a small issue regarding tests for model validations that I can't seem to find a solution to. Let's say I have a User model,
class User < ActiveRecord::Base
validates :username, :presence => true
end
and a simple test
it "should require a username" do
User.new(:username => "").should_not be_valid
end
This correctly tests the presence validation, but what if I want to be more specific? For example, testing full_messages on the errors object..
it "should require a username" do
user = User.create(:username => "")
user.errors[:username].should ~= /can't be blank/
end
My concern about the initial attempt (using should_not be_valid) is that RSpec won't produce a descriptive error message. It simply says "expected valid? to return false, got true." However, the second test example has a minor drawback: it uses the create method instead of the new method in order to get at the errors object.
I would like my tests to be more specific about what they're testing, but at the same time not have to touch a database.
Anyone have any input?
CONGRATULATIONS on you endeavor into TDD with ROR I promise once you get going you will not look back.
The simplest quick and dirty solution will be to generate a new valid model before each of your tests like this:
before(:each) do
#user = User.new
#user.username = "a valid username"
end
BUT what I suggest is you set up factories for all your models that will generate a valid model for you automatically and then you can muddle with individual attributes and see if your validation. I like to use FactoryGirl for this:
Basically once you get set up your test would look something like this:
it "should have valid factory" do
FactoryGirl.build(:user).should be_valid
end
it "should require a username" do
FactoryGirl.build(:user, :username => "").should_not be_valid
end
Here is a good railscast that explains it all better than me:
UPDATE: As of version 3.0 the syntax for factory girl has changed. I have amended my sample code to reflect this.
An easier way to test model validations (and a lot more of active-record) is to use a gem like shoulda or remarkable.
They will allow to the test as follows:
describe User
it { should validate_presence_of :name }
end
Try this:
it "should require a username" do
user = User.create(:username => "")
user.valid?
user.errors.should have_key(:username)
end
in new version rspec, you should use expect instead should, otherwise you'll get warning:
it "should have valid factory" do
expect(FactoryGirl.build(:user)).to be_valid
end
it "should require a username" do
expect(FactoryGirl.build(:user, :username => "")).not_to be_valid
end
I have traditionally handled error content specs in feature or request specs. So, for instance, I have a similar spec which I'll condense below:
Feature Spec Example
before(:each) { visit_order_path }
scenario 'with invalid (empty) description' , :js => :true do
add_empty_task #this line is defined in my spec_helper
expect(page).to have_content("can't be blank")
So then, I have my model spec testing whether something is valid, but then my feature spec which tests the exact output of the error message. FYI, these feature specs require Capybara which can be found here.
Like #nathanvda said, I would take advantage of Thoughtbot's Shoulda Matchers gem. With that rocking, you can write your test in the following manner as to test for presence, as well as any custom error message.
RSpec.describe User do
describe 'User validations' do
let(:message) { "I pitty da foo who dont enter a name" }
it 'validates presence and message' do
is_expected.to validate_presence_of(:name).
with_message message
end
# shorthand syntax:
it { is_expected.to validate_presence_of(:name).with_message message }
end
end
A little late to the party here, but if you don't want to add shoulda matchers, this should work with rspec-rails and factorybot:
# ./spec/factories/user.rb
FactoryBot.define do
factory :user do
sequence(:username) { |n| "user_#{n}" }
end
end
# ./spec/models/user_spec.rb
describe User, type: :model do
context 'without a username' do
let(:user) { create :user, username: nil }
it "should NOT be valid with a username error" do
expect(user).not_to be_valid
expect(user.errors).to have_key(:username)
end
end
end