Reputation: 1056
I have an rspec/factory girl test where I can't get a test to pass.
I'm using devise where current_user calls the currently logged in User model.
I can load up a test console and type in
u = Factory(:user)
u.company
And this will return a valid company but for some reason in rspec calling current_user.company is returning nil.
Any ideas?
Controller
class CompaniesController < ApplicationController
before_filter :authenticate_user!
def show
@company = current_user.company
end
end
Model
class User < ActiveRecord::Base
validates_uniqueness_of :email, :case_sensitive => false
has_one :company
end
Factory
Factory.define :company do |f|
f.name 'Test Company'
end
Factory.sequence(:email) do |n|
"person#{n}@example.com"
end
Factory.define :user do |f|
f.name 'Test User'
f.email {Factory.next :email}
f.password 'password'
f.company Factory(:company)
end
Test
describe CompaniesController do
before(:each) do
@user = Factory(:user)
sign_in @user
end
describe "GET show" do
before do
get :show
end
it "should find the users company" do
assigns(:company).should be_a(Company)
end
end
end
Spec Helper
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.infer_base_class_for_anonymous_controllers = false
end
Test Result
Failures:
1) CompaniesController GET show should find the users company
Failure/Error: assigns(:company).should be_a(Company)
expected nil to be a kind of Company(id: integer, name: string, user_id: integer, created_at: datetime, updated_at: datetime)
# ./spec/controllers/companies_controller_spec.rb:21:in `block (3 levels) in <top (required)>'
EDIT
I have removed the f.company = Factory(:company) in the factories file. And made my controller spec this
require 'spec_helper'
describe CompaniesController do
let(:current_user) { Factory(:user) }
before(:each) do
sign_in current_user
current_user.company = Factory(:company)
current_user.save
end
describe "GET show" do
before do
get :show
end
it "should find the users company" do
current_user.should respond_to(:company)
assigns(:company).should == current_user.company
end
end
end
Upvotes: 1
Views: 1312
Reputation: 19092
I think the main thing you were missing is setting up an association in your factory. Starting from your original example:
Factory.define :user do |f|
f.name 'Test User'
f.email {Factory.next :email}
f.password 'password'
f.association :company, factory => :company
end
Then when you create a user, it will create a company and fill in user.company_id with the proper id.
See "Associations" in the Factory Girl Getting Started doc.
Upvotes: 0
Reputation: 6171
Define Let object for company in your controller rspec.
describe CompaniesController do
describe "authorizations" do
before(:each) do
let(:company) { Factory :company }
let(:user_admin) { Factory(:user) }
end
it "should redirect" do
sign_in(user_admin)
get :show
end
it "should find the users company" do
assigns(:company).should be_a(company)
end
end
end
Can you try with above spec ?
Upvotes: 0
Reputation: 343
I'm not sure but I believe assigns(:company) checks for an instance variable @company
which obviously doesn't exist. Try putting @company = @user.company
in your before(:each)
block or test for it in another way, for example;
it "should find the users company" do
@user.should respond_to(:company)
end
I believe that should do it!
Upvotes: 1