Chuck Remes
2009-Aug-25 19:59 UTC
[rspec-users] arbitrary handling of received messages in mocks
I am trying to process a message sent to a mock to verify it contains
the correct keys. In my case I am sending a JSON string to the mock
built from data passed in via the test. The object internally builds a
hash and then constructs the JSON string from it.
I can''t get my mock to fail though. Here''s an example similar
to what
I''ve got.
describe Foo
it "should match the json string" do
# setup stubs & mocks
...
hsh = ... # keys and values used inside the method
@api.should_receive(:publish_to_bus) do |message|
false # should always fail
end
Foo.new(@api).decode hsh
end
end
The documentation says the expectation passes or fails based upon the
return value of the block. I can''t even force it to fail by returning
false. Once I figure this out, I plan to decode the JSON string in the
mock block and compare the resulting collection with the data I
originally passed in to the object. This behavior will likely become a
helper method (if that matters).
What am I doing wrong?
rspec 1.2.8
ruby 1.8.6 patch 287, Windows
Running under Netbeans 6.7.1
Thank you for your help.
cr
Ben Mabey
2009-Aug-25 22:04 UTC
[rspec-users] arbitrary handling of received messages in mocks
Chuck Remes wrote:> I am trying to process a message sent to a mock to verify it contains > the correct keys. In my case I am sending a JSON string to the mock > built from data passed in via the test. The object internally builds a > hash and then constructs the JSON string from it. > > I can''t get my mock to fail though. Here''s an example similar to what > I''ve got. > > describe Foo > it "should match the json string" do > # setup stubs & mocks > ... > hsh = ... # keys and values used inside the method > @api.should_receive(:publish_to_bus) do |message| > false # should always fail > end > > Foo.new(@api).decode hsh > end > end > > The documentation says the expectation passes or fails based upon the > return value of the block. I can''t even force it to fail by returning > false. Once I figure this out, I plan to decode the JSON string in the > mock block and compare the resulting collection with the data I > originally passed in to the object. This behavior will likely become a > helper method (if that matters).The docs could wrong.. I dunno.... When using the block form I always set expectation within the block. This may be the wrong way to use it, but it works. Like so: describe Foo it "should match the json string" do # setup stubs & mocks ... hsh = ... # keys and values used inside the method @api.should_receive(:publish_to_bus) do |message| JSON.parse(message).should == hsh # or whatever you need to do here. end Foo.new(@api).decode hsh end end -Ben
Tom Stuart
2009-Aug-25 22:13 UTC
[rspec-users] arbitrary handling of received messages in mocks
On 25 Aug 2009, at 20:59, Chuck Remes wrote:> The documentation says the expectation passes or fails based upon > the return value of the block. I can''t even force it to fail by > returning false.The docs (http://rspec.info/documentation/mocks/message_expectations.html ) say:> You can supply a block to a message expectation. When the message is > received > by the mock, the block is passed any arguments and evaluated. The > result is > the return value of the block.This means that the mocked method will return the value that is returned by the block; by returning false in your block, you''re just arranging for the mocked method to return false. (So this is the same as @api.should_receive(:publish_to_bus).and_return(false).) If you want the message expectation to fail, you need to put a failing expectation inside the block. Cheers, -Tom
Chuck Remes
2009-Aug-26 15:58 UTC
[rspec-users] arbitrary handling of received messages in mocks
On Aug 25, 2009, at 5:13 PM, Tom Stuart wrote:> On 25 Aug 2009, at 20:59, Chuck Remes wrote: >> The documentation says the expectation passes or fails based upon >> the return value of the block. I can''t even force it to fail by >> returning false. > > The docs (http://rspec.info/documentation/mocks/message_expectations.html > ) say: > >> You can supply a block to a message expectation. When the message >> is received >> by the mock, the block is passed any arguments and evaluated. The >> result is >> the return value of the block. > > This means that the mocked method will return the value that is > returned by the block; by returning false in your block, you''re just > arranging for the mocked method to return false. (So this is the > same as @api.should_receive(:publish_to_bus).and_return(false).) > > If you want the message expectation to fail, you need to put a > failing expectation inside the block.Setting an expectation within the block makes it work. I think the docs should be clearer that the expectations inside are necessary. cr