On Wed, Sep 16, 2009 at 3:34 AM, Ramon Tayag <ramon.ta...@gmail.com> wrote:
> I put an expectation test, but it complains two things are different
> when it prints it out in the error in the exact same way:
> http://pastie.org/618481

Printing is printing. Time is _not_ time. The problem is that the
times are off by milliseconds that aren't accounted for in time.to_s.

The usual solution is to either stub Time.now or introduce a time
generator, but you're not using Time.now, so that won't work. Options
include:

1. examine the submitted argument directly

@orders.should_receive(:updated_on) do |actual|
  expected = 1.week.ago.to_i
  actual.to_i.should be_between(expected - 1000, expected)
end

That will expect the time within 1 second of 1.week.ago.

2. introduce a custom argument matcher

class Roughly
  def initialize(expected)
    @expected = expected.to_i
  end

  def ==(actual)
    actual.to_i.between?(@expected - 1000, @expected)
  end
end

def roughly(expected)
  Roughly.new(expected)
end

@orders.should_receive(:updated_on).with(roughly(1.week.ago))

And for extra credit, you can even make this one more flexible and
provide a lower and upper bound using a fluent interface:

@orders.should_receive(:updated_on).with(within(1.second).of(1.week.ago))

HTH,
David

>
> or below:
>
> ## the test
>  describe ".process_aftersale" do
>    before do
>     �...@orders = [Order.make, Order.make]
>      Order.stub!(:paid).and_return(@orders)
>     �...@orders.stub!(:updated_on).and_return(@orders)
>    end
>
>    ....
>
>    it "should send 1 week after sale letters" do
>     �...@orders.should_receive(:updated_on).once.with([1.week.ago])
>      Order.process_aftersale
>    end
>
>    ...
>  end
>
> ## The error - note that the dates are the same
> 1)
> Spec::Mocks::MockExpectationError in 'Order.process_aftersale should
> send 1 week after sale letters'
> #<Order:0xb4e8b3a4>#<Order:0xb4e69448> expected :updated_on with
> ([Wed, 09 Sep 2009 14:59:59 HKT +08:00]) but received it with ([Wed,
> 09 Sep 2009 14:59:59 HKT +08:00])
> ./spec/models/order_spec.rb:42:
>
>
> ## the method in Order
>  def self.process_aftersale
>    self.paid.updated_on(1.week.ago).each do |o|
>      MailingsWorker.asynch_deliver_order_aftersale_to_inquire(:order_id
> => o.id)
>    end
>  end
>
> ==============
>
> I may be doing things wrong, so please let me know if I am.
>
> Thanks!
> Ramon Tayag
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to