ruby - RSpec2 中的模拟导致单例无法转储到 Sinatra Controller 中

标签 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

这是我的 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/

相关文章:

ruby - 在 Jekyll 驱动的站点上创建类别

ruby - 在 Ruby 中重新排列一维列表

php - 我是一名转换为 python 的 php 程序员。请解释单元测试的重要性(或不重要)。我从来没有用 php 进行过单元测试!

java - 如何在 Hibernate 中修复 'A rollback database operation after a JUnit test'(没有 Spring)?

android - 如何在 Android 测试中正确模拟 HttpGet 调用

ruby-on-rails - 在 Rails 中创建静态页面的最佳方式是什么?

ruby-on-rails - Time.zone.at 比 Time.at 慢 10 倍

c# - ClassInitialize : no longer working in Visual Studio 2012 中生成的数据驱动测试

c# - 使用 Moq 模拟带有条件参数的方法

javascript - 访问具有数组的 json 对象