【问题标题】:Mock "" received unexpected message模拟“”收到意外消息
【发布时间】:2013-03-27 20:24:52
【问题描述】:

我有以下代码

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")

access_token 方法在 query_account 中被调用。有谁知道我也可以模拟它来克服这个问题。

【问题讨论】:

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


    【解决方案1】:

    您正在存根 OAuth::Consumer.new 以返回您的模拟,但您没有在您的模拟上存根任何东西,因此它没有 #get_access_token 方法(或任何其他方法)。您必须对要在模拟中使用的方法存根。

    oauth_mock = mock
    oauth_mock.stubs(:get_access_token).returns(whatever)
    Oauth::Consumer.stubs(:new).returns(oauth_mock)
    

    【讨论】:

    • 嘿吉姆...这有帮助..谢谢...没有意识到我两次发布了这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多