我有一个Rails服务器运行在Thin和一个异步控制器动作上。我想用RSpec的测试,但我得到了一个错误:测试Rails控制器使用throw的动作的正确方法是什么:异步?
Failure/Error: get :access_token
ArgumentError:
uncaught throw :async
这是一个示例代码:
class SampleController < ApplicationController
def sample
EM.defer do
render json: { response: 'Hello World' }
request.env['async.callback'].call response
end
throw :async
end
end
require 'spec_helper'
describe SampleController, :type => :controller do
it "hello world" do
get :sample
expect(JSON.parse(response.body)[response]).to eq('Hello World')
end
end
为什么我得到这个错误的原因是:异步只能是由瘦服务器处理。在vanilla Rails控制器中没有Thin服务器可用。
所以试图水豚:
describe "GET /sample", type: feature do
it "hello world" do
visit sample_path
expect(JSON.parse(page.body)['response']).to eq('Hello World')
end
end
# In spec_helper.rb
Capybara.server do |app, port|
require 'rack/handler/thin'
Rack::Handler::Thin.run(app, :Port => port)
end
但我仍然得到同样的错误。我相信这是因为Thin需要以线程模式启动;水豚不会以这种方式开始。
测试控制器使用throw的动作的正确方法是什么:异步?
当我使用常规浏览器进行操作时,该操作确实有效。
这使测试失败。 'NoMethodError:未定义方法'调用'为nil:NilClass'。这来自调用'request.env ['async.callback']。call response' – Nerian 2014-09-30 11:10:07
需要完整的回溯帮助。 – 2014-10-01 04:21:44
/Users/Nerian/projects/async_test/app/controllers/application_controller.rb:10:in'block in example_async':未定义方法调用为nil:NilClass(NoMethodError) #from /Users/Nerian/.rvm/ gems/ruby-2.1.3/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:在'call'中 #from /Users/Nerian/.rvm/gems/ruby-2.1.3/gems/eventmachine -1.0.3/lib/eventmachine.rb:1037:在'block in spawn_threadpool' – Nerian 2014-10-01 10:56:18