0

我正在尝试在我的一个 Rails 控制器(在本例中为 Instagram.get_access_token)中模拟对外部 API 的方法调用,但遇到了一些麻烦。如所写,代码仍在调用真正的 Instagram.get_access_token 方法。我如何让控制器使用我的简单模拟?

session_controller.rb:

class SessionsController < ApplicationController

  require 'instagram'
  include ApplicationHelper

  def auth_callback
    response = Instagram.get_access_token(params[:code],
                                          redirect_uri: auth_callback_url)
    #<snipped extra code>
  end
end

session_controller_spec.rb:

require 'spec_helper'
require 'ostruct'

describe SessionsController do
  describe "GET #auth_callback" do
    context "when there is an existing user" do
      let(:response) { OpenStruct.new(access_token: "good_access_token") }

      it "parses an access_token from the get response" do
        Instagram.should_receive(:get_access_token).and_return(response)

        #<snipped extra code>
      end
    end
  end
end
4

1 回答 1

0

尝试:

Instagram.stub(:get_access_token) { response }

你有同样的结果吗?

于 2013-05-18T20:54:45.583 回答