On Aug 27, 2009, at 12:22 PM, Matt Wynne wrote:
On 27 Aug 2009, at 17:02, Chuck Remes wrote:
Let's assume that method1 and method2 are acting upon other objects
that I can also mock via DI. How can I generate the 'StartEvent' so
that the block above is executed? BTW, the layers are as separated
as they are going to get. In the WIN32OLE_EVENT class you *must*
pass a block as shown above. I don't know how to get that block to
execute in my specs without actually connecting to the COM service
and getting it to generate that 'StartEvent'. Do you know of
another way?
it "should execute the block upon receipt of event 'StartEvent'" do
handler = mock("event handler")
handler.should_receive(:on_event).with('StartEvent') # necessary?
obj1_mock = mock("obj1")
obj1.should_receive(:method1)
foo = Foo.new
foo.obj1 = obj1
foo.bar
# callback has been set; how do I generate the 'StartEvent'
# to run that block?
foo.???
end
So I'm not sure if you can simulate a block being yeilded with
RSpec's mocks, it's probably possible but I think you'd be simpler
using a hand-made fake:
class FakeEventHandler
def initialize(*args_to_yield)
@args_to_yield = *args_to_yield
end
def on_event(event_name)
yield *...@args_to_yield
end
end
Something like that.
Point being, a test-double (fake) doesn't always have to be a mock
object.
<slaps forehead>
I often forget this. I have written fakes in the past using just this
technique but it's so rare that I had forgotten about it again. I
usually remember things permanently after I have had to rediscover
them 4 or 5 times. :)
Thanks for your insight. This technique will cure my testing problem.
cr
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users