0

Consider the following class and methods: (This class is obviously much more complete, but for the sake of this thread...):

class Order < ActiveRecord::Base

  def check
    if (self.user.phone == "55555555") do
      self.a_certain_method
      return
    end
  end

  def a_certain_method
    # Real implementation goes here
  end

end

And the following Unit Test:

describe :do_route do
it "should call a_certain_method if user phone number matches 55555555" do
  # Create a user
  user = Factory(:user)

  # Set hard-coded phone number
  user.phone = "55555555"
  user.save!

  # Create an order made by the ordering user
  order = Factory(:order, :ordering_user => user)

  # Set expectation for a "a_certain_method" call
  mock(order).a_certain_method

  # Call the tested method
  order.check
end
end

From some reason, the above test produces an RR::Errors::TimesCalledError error, which claims that a_certain_method was called 0 times instead of 1... I've been searching around the web for a solution with no luck.

I've tried building a similiar test on a non-activerecord class, and the test produces no errors. I've used the debugger to check that it does reach the self.a_certain_method line, and also tried using the following instead of mock(order).a_certain_method:

  any_instance_of(Order) do |o|
    mock(o).a_certain_method
  end

Does anyone have any idea how to solve this issue since i'm kind of desperate...

Mikey S.
  • 3,301
  • 6
  • 36
  • 55
  • I've tried to reproduce your issue without success. Can you add code of your `Gemfile` and definition of your factories for user and order? Also try to change user initialization in your test to `user = Factory(:user, :name => "55555555")`, will it pass? – Aliaksei Kliuchnikau Dec 13 '11 at 19:00
  • I figured out what the problem was, it failed since the number was already in the database. so it failed to save the hard coded user.phone change. Thanks for the help though :) – Mikey S. Dec 14 '11 at 12:46
  • Can you please post this comment as an answer and accept it so your question won't be considered by StackOverflow as unanswered. Also is your question http://stackoverflow.com/q/8464376/158689 answered too? Thanks! – Aliaksei Kliuchnikau Dec 14 '11 at 13:12
  • I meant accept your own answer is absolutely fine. – Aliaksei Kliuchnikau Dec 14 '11 at 13:51

1 Answers1

0

I figured out what the problem was, it failed since the number was already in the database. so it failed to save the hard coded user.phone change.

Thanks for the help though :)

Mikey S.
  • 3,301
  • 6
  • 36
  • 55