我在创建测试以模拟google-api-client gem时遇到了困难。以下是我试图测试的方法。
此方法预计会为经过身份验证的用户返回所有Google日历。
def all_google_calendars
#create a google api client
client = Google::APIClient.new
#assign the google client an access token recieved during oauth
client.authorization.access_token = session[:oauth_token]
#specify the google api we wish to use
service = client.discovered_api('calendar', 'v3')
#execute google api method to get users calendar information
result = client.execute(:api_method => service.calendar_list.list)
#variable with only relevent calendar data
entries = result.data.items
#empty array to store calendar names
calendar_events = []
#iterate through each entry and extract summary. summary == name of calendar
entries.each do |e|
calendar_events.push 'name' => e.summary, 'id' => e.id
end
calendar_events
end
这是我的rspec测试的开始:
let(:session) {{oauth_token: 'fake_token'}}
describe '#all_google_calendars' do
context 'when requesting a google calendar list' do
it 'returns a users google calendars' do
client = double('client')
allow(client).to receive_message_chain('authorization.access_token')
Google::APIClient = object_double(Google::APIClient, :new => client)
allow(double).to receive_message_chain(:authorization, :access_token) {session[:oauth_token]}
allow(Google::APIClient).to receive_message_chain(:new, :authorization, :access_token => session[:oauth_token]) { session[:oauth_token] }
expect(controller.all_google_calendars).to eq(session[:oauth_token])
end
end
end
我收到错误
Double received unexpected message :access_token= with (nil)
为什么这条链式消息不会被嘲笑? 谢谢你的帮助!