0.0
The project is in a healthy, maintained state
RSpec matcher for testing method invocations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

InvokeMatcher

This is adapted from: rspec/rspec-expectations#934

It allows us to write expectations in a more declarative way, by checking if a method was called on a class or instance.

Old way:

allow(foo).to receive(:method).and_return('value')
subject
expect(foo).to have_received(:method)

Or:

expect(foo).to receive(:method).and_return('value')
subject

New way:

expect { subject }.to invoke(:method).on(foo).with('bar').and_return('value')

Installation

Add this to your test gems:

group :test do
  gem 'invoke_matcher'
end

Make sure to require it in your spec_helper.rb or rails_helper.rb:

require 'invoke_matcher'

RSpec.configure do |config|
  config.include InvokeMatcher
end

Usage

expect { foo }.to invoke(:method).on(Class).and_call_original

expect { foo }.to change{ bar }.and not_invoke(:method).on(Class)

expect { foo }.to invoke(:method).on(Class).at_least(3).times

expect { foo }.to invoke(:method).and_expect_return('bar')

License

The gem is available as open source under the terms of the MIT License.