How to Expect Some (But Not All) Arguments with Rspec Should_Receive

How to expect some (but not all) arguments with RSpec should_receive?

Use the anything matcher:

Foo.should_receive(:bar).with(:baz, anything)

rspec should_receive is not working but expect is working

place this line:

customer1.should_receive(:my_money)

before

expect(Bank.new.transfer(customer1, customer2, 2000)).to eq("Insufficient funds")

expect to have_received and should_receive have diffent meaning

expect to have_received passes if object already received expected method call while
should_receive passes only if object will receive expected method call in future (in scope of current testcase)

if you would write

expect(customer1).to receive(:my_money)

instead of

expect(customer1).to have_received(:my_money)

it would fail too. Unless you place it before the line which calls this method.

RSpec `should_receive` behaviour with multiple method invocation

This should work:

class A
def print(args)
puts args
end
end

describe A do
let(:a) {A.new}

it "receives print" do
a.stub(:print).with(anything())
a.should_receive(:print).with("World").and_call_original

a.print("Hello")
a.print("World")
end
end

The test was failing because you had set a precise expectation "a should receive :print with 'World'", but rspec noticed that the a object was receiving the print method with 'Hello' therefore it failed the test. In my solution, I allow the print method to be invoked with any argument, but it still keeps track of the call with "World" as argument.

rspec: 'should_receive' with multiple argument expectations

You need to provide a custom matcher, but you can readily define your error reporting so that you can give specifics about what failed and why. See https://github.com/dchelimsky/rspec/wiki/Custom-Matchers .

In particular, the custom matcher would be supplied as the argument to with, as mentioned in the last sentence of the first paragraph of the "Argument Matchers" section of https://github.com/rspec/rspec-mocks.

As for error reporting, there are no custom failure methods that apply to this use case, but the description method of the custom matcher is used generate the string shown as the "expected" value and, though not its purpose, can be defined to output anything you want regarding the failed match.

How can I add multiple should_receive expectations on an object using RSpec?

Multiple expectations are not a problem at all. What you're running into are ordering problems, given your specific args on unordered expectations. Check this page for details on ordering expectations.

The short story is that you should add .ordered to the end of each of your expectations.

should_receive in RSpec

Easy:

User.should_receive(:all).once

What I want is to check, if a certain Class (not object) received a certain message

A class is an object!

Object.any_instance should_receive vs expect() to receive

There's now a not very well documented method called expect_any_instance_of that handles the any_instance special case. You should use:

expect_any_instance_of(Object).to receive(:subscribe)

Google expect_any_instance_of for more info.

How to say should_receive more times in RSpec

This is outdated. Please check Uri's answer below

for 2 times:

Project.should_receive(:find).twice.with(@project).and_return(@project)

for exactly n times:

Project.should_receive(:find).exactly(n).times.with(@project).and_return(@project)

for at least n times:

Project.should_receive(:msg).at_least(n).times.with(@project).and_return(@project)

more details at https://www.relishapp.com/rspec/rspec-mocks/v/2-13/docs/message-expectations/receive-counts under Receive Counts

Hope it helps =)

How to say any_instance should_receive any number of times in RSpec

There's a new syntax for this:

expect_any_instance_of(Model).to receive(:save).at_least(:once)


Related Topics



Leave a reply



Submit