Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:32:25+00:00 2026-05-23T17:32:25+00:00

I have an Employee model, Client model, and an Office model. Devise is controlling

  • 0

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, :type => :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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:32:26+00:00Added an answer on May 23, 2026 at 5:32 pm
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Domain model: Employee: Id, FirstName, LastName, Sex, BirthDate. Office: Employee, WorkStation,
Hi I'm have an object model with an Employee and EmployeeWorksiteReference (cause it's an
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
I have a controller Employee , in that i have the action detail .
I have an Employee model that has a Name and Company I want to
I have a model class Employee_Type(models.Model): def __unicode__(self): return self.name name = models.CharField(max_length=200, verbose_name=employee
I have an Entity Framework model with table Employees. Each employee has a SupervisorId,
I have an employee entity in my EF model. I then added a class
I have a Job model that belongs_to three users (customer, employee, and qa). The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.