[rspec-users] Cucumber - stub! or mock?

Pat Maddox pergesu at gmail.com
Tue Sep 30 12:04:50 EDT 2008


Scott Taylor <mailing_lists at railsnewbie.com> writes:

> On Sep 30, 2008, at 11:06 AM, David Chelimsky wrote:
>
>> On Tue, Sep 30, 2008 at 9:55 AM, Luke Melia <luke at lukemelia.com>
>> wrote:
>>> On Sep 15, 2008, at 4:35 PM, Pat Maddox wrote:
>>>
>>>> I don't think you're really supposed to mock or stub when using
>>>> cucumber.
>>>
>>> We need to stub time in some of our scenarios, which exist to to
>>> verify
>>> behavior over time. We're looking into a before/after to support
>>> mocking/stubbing for this scenario.
>>
>> There's no direct hooks into the mock framework and I don't think
>> there should be, but you can roll your own in the supplied
>> Before/After methods:
>>
>> Before do
>>  #do some magic w/ time
>> end
>>
>> After do
>>  #undo some magic w/ time
>> end
>>
>> Those run before and after every scenario.
>
> How would you do this?  I guess you could just require spec/mocks/
> mock, mock / stub as usual, and then in the After block call
> Spec::Mocks::Space#reset_all ?

You can do magic without mocks...

orig_now = Time.method(:now)
Before do
  now = Time.now
  (class << Time; self; end).send(:define_method, :now) { now }
end

After do
  (class << Time; self; end).send(:define_method, :now) { orig_now.call }
end

Pat


More information about the rspec-users mailing list