在RSpec2中进行模拟导致单例不能在Sinatra控制器中转储

时间:2013-07-01 07:07:31

标签: ruby unit-testing mocking sinatra rspec2

我正在尝试编写一个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

这是我的控制器

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)>'

不确定我错过了什么。

1 个答案:

答案 0 :(得分:2)

为了在会话中粘贴某些东西,它需要被序列化,并且你的模拟对象不能被序列化 - rspec模拟的实现似乎使用单例或定义单例方法

你可以尝试找出你需要什么样的方法来假装可以转储对象(也许是dump),我个人只是让测试请求令牌成为类似的结构。