模拟“”收到了意外的消息

时间:2013-03-27 20:24:53

标签: ruby-on-rails ruby-on-rails-3.1 ruby-on-rails-3.2

我有以下代码

Class Client 
  def initialize(options = {})
    @key = options['oauth_key']
    @secret = options['oauth_secret']
    @access_token_url = options['oauth_access_token_url']
    @signature_method = options['signature_method']
    @consumer = OAuth::Consumer.new(@key, @secret, {access_token_url: @access_token_url, signature_method: @signature_method})
  end

  def accounts_by_id(account_id)
    response = query_account(account_id)
    parse_json(response)
  end

  private
  def access_token()
    ...
    ...
    ... 
    @access_token = @consumer.get_access_token(nil)
    ...
  end

消费者被嘲笑如下

oauth_mock = mock('oauth')
OAuth::Consumer.stubs(:new).returns(oauth_mock)
然而,当我做

时,我得到了模拟“oauth”收到意外消息:get_access_token with (nil)
GameSystem::Client.new(oauth_key: 'KEY',oauth_secret: 'SECRET',oauth_access_token_url: 'http://localhost').accounts_by_id("kk")

在query_account中调用access_token方法。任何人都知道如何模拟它以克服这个问题。

1 个答案:

答案 0 :(得分:2)

你正在使用OAuth::Consumer.new来返回你的模拟,但是你没有在你的模拟上存在任何东西,所以它没有#get_access_token方法(或任何其他方法)。你必须在mock上存储你想要使用的方法。

oauth_mock = mock
oauth_mock.stubs(:get_access_token).returns(whatever)
Oauth::Consumer.stubs(:new).returns(oauth_mock)
相关问题