【发布时间】:2010-11-17 20:35:37
【问题描述】:
我正在尝试在 Rails 2.3.10 上测试连接到 Google 以检索联系人的控制器操作。我正在使用 Rspec 和 Mocha 进行测试。因为这是一个单元测试,所以我想删除对 Google 的实际调用。我想验证是否使用正确的参数调用了 authsub_url 方法。将方法存根会导致期望失败。
任何建议将不胜感激。
谢谢!
我将客户端设置为 google 的方法如下:
def setup_client
@client = GData::Client::DocList.new(:authsub_scope => CONTACTS_SCOPE, :source => 'google-DocListManager-v1.1', :version => '3.0')
if params[:token].nil? && session[:google_token].nil?
@authsub_link = @client.authsub_url(import_method_gmail_url, false, true)
render :action => :index, :layout => "empty"
elsif params[:token] && session[:google_token].nil?
@client.authsub_token = params[:token]
session[:google_token] = @client.auth_handler.upgrade
end
@client.authsub_token = session[:google_token] if session[:google_token]
end
这是我的测试:
describe "setup_client" do
it "has a authsub_link if there is no token parameter and the google token is not present in the session" do
GData::Client::DocList.any_instance.stubs(:authsub_url).returns("http://test.google.com/contacts")
user = Factory(:subscriber_user)
profile = Factory(:profile, :user => user)
login_as user
controller.instance_variable_get(:@client).expects(:authsub_url).with(import_method_gmail_url, false, true).once
get :index
assigns(:authsub_link).should == "http://test.google.com/contacts"
end
end
【问题讨论】:
标签: ruby-on-rails rspec mocha.js