I have an Employee model, Client model, and an Office model. Devise is controlling the authentication logic on the Employee. I have multiple controllers which are subclassing a base controller which sets everything needed for all controllers (DRY). When testing these controllers, I need to be able to mock a returned office on the signed in devise employee in order for the controller tests to function properly, but am having issues.
The question: how can I mock User#office to return mock_office on the current_employee when current_employee is a real object.
Here is all the code needed to describe my problem. app/models/employee.rb
class Employee < ActiveRecord::Base
devise :database_authenticatable, :token_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :lockable, :timeoutable
attr_accessible :username, :password_confirmation, :password, :email, :office_id
belongs_to :office
end
app/models/client.rb
class Client < ActiveRecord::Base
belongs_to :office
end
app/models/office.rb
class Office < ActiveRecord::Base
has_many :employees
has_many :clients
end
config/routes.rb
Project::Application.routes.draw do
devise_for :employees
namespace :employees do
resources :clients
end
end
app/controllers/employees/base_controller.rb
class Employees::BaseController < ApplicationController
before_filter :authenticate_employee! && :set_office
private
def set_office
@office = current_employee.office
end
end
app/controllers/employees/clients_controller.rb
class Employees::ClientsController < Employees::BaseController
def create
flash[:notice] = Message::SOME_MESSAGE
end
end
spec/support/controller_macros.rb
module ControllerMacros
def login_employee
let(:current_employee) { Factory(:employee) }
let(:mock_office) { mock_model Office }
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:employee]
sign_in :current_employee, current_employee
current_employee.should_receive(:office).and_return(:mock_office)
end
end
end
spec/spec_helper.rb
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :t开发者_如何学JAVAype => :controller
end
end
Spork.each_run do
end
spec/controllers/employees/clients_controller_spec.rb
require 'spec_helper'
describe Users::ClientsController do
login_employee
it { should inherit_from(Users::Admin::BaseController) }
describe 'basic test' do
before do
post :create, {}
end
it {should respond_with_content_type(:html)}
end
end
results of running the specs
(in /Users/developer/Development/Workspace/Project.ror3) /Users/developer/.rvm/rubies/ruby-1.9.2-p180/bin/ruby -S bundle exec rspec ./spec/controllers/employees/clients_controller_spec.rb No DRb server is running. Running in local process instead ... .F Failures: 1) Users::ClientsController basic test Failure/Error: post :create, {} NoMethodError: undefined method `office' for nil:NilClass # ./app/controllers/employees/base_controller.rb:6:in `set_office' # ./spec/controllers/employees/clients_controller_spec.rb:12:in `block (3 levels) in ' Finished in 1.09 seconds 2 examples, 1 failure Failed examples: rspec ./spec/controllers/employees/clients_controller_spec.rb:15 # Users::ClientsController basic test rake aborted! ruby -S bundle exec rspec ./spec/controllers/employees/clients_controller_spec.rb failed (See full trace by running task with --trace)
NOTE: There was a lot of code slimming to get all the information above. I re-read many times and believe the problem is not in the setup but the devise object itself (using sign_in). If you need more, please request.
module ControllerMacros
def login_employee
let(:current_employee) { mock_model Employee }
let(:mock_office) { mock_model Office }
before(:each) do
request.env['warden'] = mock(Warden, :authenticate => current_employee,
:authenticate! => current_employee)
current_employee.should_receive(:office).and_return(:mock_office)
end
end
end
精彩评论