2013-03-27 41 views
0

我有以下代碼模擬「」收到意外的消息

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)當我這樣做a

GameSystem::Client.new(oauth_key: 'KEY',oauth_secret: 'SECRET',oauth_access_token_url: 'http://localhost').accounts_by_id("kk") 

access_token方法在query_account中被調用。是否有人知道ho我也可以嘲笑這個來克服這個問題。

回答

2

您將OAuth::Consumer.new存檔以返回您的模擬,但您不會在您的模擬中存留任何內容,因此它沒有#get_access_token方法(或任何其他方法)。你必須在模擬中存根你想使用的方法。

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

嘿吉姆...這有助於......謝謝你......沒有意識到我發佈了兩次問題 – user2217348 2013-03-28 18:50:29