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
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to