ruby on rails - Mocha mocking class method include before_filter -


i'm trying test before_filter being called concern. test looks this:

class authorizablecontroller < applicationcontroller   include authorizable end  describe authorizable   let(:dummy) { authorizablecontroller.new }    "adds before filter class"     authorizablecontroller.expects(:before_filter).with(:authorize)      dummy.class_eval |klass|       include authorizable     end   end end 

my concern looks so:

module authorizable   extend activesupport::concern    included     before_filter :authorize   end end 

...and i'm getting error looks (no mention of mocha, , instead minitest, when i'm using rspec...):

failures:    1) authorizable adds before filter class      failure/error: authorizablecontroller.expects(:before_filter).with(:authorize)      minitest::assertion:        not expectations satisfied        unsatisfied expectations:        - expected once, not yet invoked: authorizablecontroller.before_filter(:authorize)      # ./spec/controllers/concerns/authorizable_spec.rb:11:in `block (2 levels) in <top (required)>' 

the rails method class_eval evaluates on singleton class of object in question not concrete class. there's more of explanation in this question of singleton class is. because filter being added singleton class expectation of authorize being called on main class isn't met.

you add expectation on singleton class dummy:

dummy.singleton_class.expects(:before_filter).with(:authorize)  

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -