ruby - Custom Matcher for "calls method on object" -


given method like:

class myclass   def method_that_calls_stuff     method2("some value")   end end 

i'd define expectation like:

my_object = myclass.new expect{ my_object.method_that_calls_stuff }.to call(:method2).on(my_object).with("some value") 

i know can achieve same thing using rspec-mocks, don't syntax well.

how define matcher (or better, has written one)?

with new syntax though can get

instance = myclass.new expect(instance).to receive(:method2).with("some value") instance.method_that_calls_stuff 

but if want matcher, can do

rspec::matchers.define(:call) |method|   match |actual|     expectation = expect(@obj).to receive(method)     if @args       expectation.with(@args)     end     actual.call     true   end    chain(:on) |obj|     @obj = obj   end    chain(:with) |args|     @args = args   end    def supports_block_expectations?     true   end end 

notice with optional may want call method without args.

you can full info on how build custom matchers here, , fluent interface/chaining here , block support here. if browse around can find how add nice error messages , such, come in handy.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -