0

I'm trying to test that before_filter is being called from a concern. My test looks like this:

class AuthorizableController < ApplicationController
  include Authorizable
end

describe Authorizable do
  let(:dummy) { AuthorizableController.new }

  it "adds a before filter to the class" do
    AuthorizableController.expects(:before_filter).with(:authorize)

    dummy.class_eval do |klass|
      include Authorizable
    end
  end
end

My concern looks like so:

module Authorizable
  extend ActiveSupport::Concern

  included do
    before_filter :authorize
  end
end

...and I'm getting an error that looks like this (no mention of mocha, and instead MiniTest, when I'm using RSpec...):

Failures:

  1) Authorizable adds a before filter to the class
     Failure/Error: AuthorizableController.expects(:before_filter).with(:authorize)
     MiniTest::Assertion:
       not all expectations were satisfied
       unsatisfied expectations:
       - expected exactly once, not yet invoked: AuthorizableController.before_filter(:authorize)
     # ./spec/controllers/concerns/authorizable_spec.rb:11:in `block (2 levels) in <top (required)>'
Charles
  • 50,943
  • 13
  • 104
  • 142
Jamie Rumbelow
  • 4,967
  • 2
  • 30
  • 42

1 Answers1

0

The rails method class_eval evaluates on the singleton class of the object in question not the concrete class. There's more of an explanation in this question of what a singleton class is. Because the filter is being added to that singleton class your expectation of authorize being called on the main class isn't met.

You could add the expectation on the singleton class for dummy:

dummy.singleton_class.expects(:before_filter).with(:authorize) 
Community
  • 1
  • 1
Shadwell
  • 34,314
  • 14
  • 94
  • 99