Вопрос

I know that this maybe really simple, I was wondering how can I test that a method is called only once in rails?

For example I have a model Customer which has a method set_account . I want to know that after some code, it will also run this method but only one time. How can I do that?

I am using rspec.

Это было полезно?

Решение

at_least_once is the way you do it in Mocha.

I believe with rspec you just add a .once at the end

  class Account
      attr_accessor :logger

      def open
          logger.account_opened
       end
  end

  describe Account do
    context "when opened" do
    it "logger#account_opened was called once" do
      logger = double("logger")
      account = Account.new
      account.logger = logger

      logger.should_receive(:account_opened).once

      account.open
    end
  end
end

that example is from here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top