0

次のクラスとメソッドを検討してください:(このクラスは明らかにはるかに完全ですが、このスレッドのために...):

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

そして、次のユニットテスト:

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

何らかの理由で、上記のテストでRR::Errors::TimesCalledErrorエラーが発生し、1回ではなく0回呼び出されたと主張してa_certain_methodいます...私は運が悪かった解決策をWebで探していました。

非アクティブレコードクラスで同様のテストを作成しようとしましたが、テストでエラーは発生しません。デバッガーを使用して、ラインに到達することを確認しましたself.a_certain_method。また、次の代わりに次のコマンドを使用してみましたmock(order).a_certain_method

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

私は必死なので、誰かがこの問題を解決する方法を知っていますか...

4

1 に答える 1

0

番号がすでにデータベースにあったため、問題が何であるかを突き止めました。そのため、ハードコードされた user.phone の変更を保存できませんでした。

でも助けてくれてありがとう:)

于 2011-12-14T13:51:30.257 に答える