RSpec: Only stub a method when a particular argument is passed
To only stub a method call if a given argument is used, but use the default implementation for other arguments:
object.should_receive(:some_method).and_call_original
object.should_receive(:some_method).with('my argument').and_return('other value')
Requires rspec-mocks
2.13+.
Related cards:
RSpec: Ensuring a method is called on an object that will be created in the future
rspec >= 3.1 brings a method and_wrap_original
. It seems a bit complicated at first, but there are use cases where it helps to write precise tests. For example it allows to add expectations on objects that will only be created when your code is ...
RSpec: Applying stubs only within a block
When you mocked method calls in RSpec, they are mocked until the end of a spec, or until you explicitly release them.
You can use RSpec::Mocks.with_temporary_scope
to have all mocks applied inside a block to be released when the block ends.
Exa...
Configure RSpec to raise an error when stubbing a non-existing method
You can configure RSpec 3.3+ to raise an error when attempting to stub or mock a non-existing method. We strongly recommend to do this as non-verified stubs are a footgun.
You can enable this behavior by adding the following to your `spec_helper....
Mocks and stubs in Test::Unit when you are used to RSpec
We are maintaining some vintage projects with tests written in Test::Unit instead of RSpec. Mocks and stubs are not features of Test::Unit, but you can use the Mocha gem to add those facilities.
The following is a q...
Fix a spec that only runs when called directly
When a spec only runs when it is called directly, but not as part of the whole test suite, make sure the filename is foo_spec.rb
instead of just foo.rb
.
Stub methods on any instance of a class in Rspec 1 and Rspec 2
RSpec 1 (Rails 2)
With the most recent spec_candy.rb
helpers you can say:
User.stub_any_instance(:foo => :bar)
user = User.new
u...
RSpec: Stubbing a method that takes a block
If you stub
a method or set expectations with should_receive
these stubbed methods may also yield blocks. This is handy if the returning object is receiving a block call.
Consider this, where you cannot say and_return []
because of the bloc...
RSpec: Expecting non-primitive objects as method invocation arguments
Expecting a primitive value as an argument to a method invocation is easy:
expect(object).to receive(:foo).with('arg1', 'arg2')
This expectation would be met with this call:
object.foo('arg1', 'arg2')
But what if the ar...
Rspec 3: what to do when `describe` is undefined
When tests might not run with skipping RSpec
in the RSpec.describe
failing with the error undefined method 'describe' for main:Object
this card will help you out!
In RSpec 3 the DSL like describe
is [exposed globally](https://rspec.info/f...
Rspec: Complex argument expectations for should_receive
Sometimes you need complex expectations on method arguments like this
SomeApi.should_receive(:find).with(:query => '*foo*', :sort => 'timestamp ASC', :limit => 100).and_return(['some result'])
This is not very flexible, and failure m...