我正在尝试编写一个 rspec2 测试,但它给了我一个错误。我知道现在的测试并没有测试任何特别的东西。但稍后我会添加更多代码,我想先通过这部分。
context "/login/twitter" do
before(:each) do
request_token = double("request_token")
request_token.stub(:authorize_url).and_return("http://api.twitter.com/oauth/authenticate?oauth_token")
TwitterService.any_instance.stub(:authentication_request_token).and_return(request_token)
get '/login/twitter'
end
it "should redirect to twitter authorized url" do
last_response.header["Location"].should include "http://api.twitter.com/oauth/authenticate?oauth_token"
end
it "should redirect back to home page if error occurs" do
end
end
这是我的 Controller
get '/login/twitter' do
begin
request_token = TwitterService.new.authentication_request_token
session[:request_token_twitter] = request_token
logger.info(request_token.authorize_url)
redirect request_token.authorize_url
rescue Exception => e
logger.error(e.message)
redirect '/'
end
end
这是我得到的错误
1) Server /login/twitter should redirect to twitter authorized url
Failure/Error: get '/login/twitter'
TypeError:
singleton can't be dumped
# ./spec/twitter_route_spec.rb:25:in `block (3 levels) in <top (required)>'
不确定我错过了什么。
最佳答案
为了将某些内容保留在 session 中,它需要被序列化,并且您的模拟对象无法被序列化 - rspec 模拟的实现似乎使用单例或定义单例方法
您可以尝试找出需要 stub 哪些方法才能假装可以转储对象(也许dump
),我个人只会将测试请求 token 设为一个结构体类似的东西。
关于ruby - RSpec2 中的模拟导致单例无法转储到 Sinatra Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17399079/