我写了一个完整的rails引擎,并试图用RSpec测试我的控制器。
仅在调用render
方法时发生错误。在return
或redirect
方法上,一切正常。而问题只与测试有关。我使用虚拟应用程序。
堆栈:
# Failure/Error: get(:search, params)
ActionView::Template::Error:
undefined method `config' for #<Rails::App:0x000000034d9ee8>
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.15/lib/rails.rb:78:in `root'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/template.rb:175:in `inspect'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/template.rb:334:in `identifier_method_name'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/template.rb:330:in `method_name'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/template.rb:267:in `compile'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/template.rb:244:in `block in compile!'
# <internal:prelude>:10:in `synchronize'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/template.rb:232:in `compile!'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/template.rb:144:in `block in render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications.rb:123:in `block in instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications.rb:123:in `instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/template.rb:143:in `render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications.rb:123:in `block in instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications.rb:123:in `instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/template_renderer.rb:45:in `render_template'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/template_renderer.rb:18:in `render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/renderer.rb:36:in `render_template'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_view/renderer/renderer.rb:17:in `render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/abstract_controller/rendering.rb:110:in `_render_template'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/streaming.rb:225:in `_render_template'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/abstract_controller/rendering.rb:103:in `render_to_body'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/renderers.rb:28:in `render_to_body'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/abstract_controller/rendering.rb:88:in `render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/rendering.rb:16:in `render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
# /home/shooma/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/core_ext/benchmark.rb:5:in `ms'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/instrumentation.rb:40:in `block in render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.15/lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/instrumentation.rb:39:in `render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/implicit_render.rb:10:in `default_render'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/implicit_render.rb:5:in `send_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/abstract_controller/base.rb:167:in `process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:403:in `_run__3295526729938120009__process_action__2756155078140629527__callbacks'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:405:in `__run_callback'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/abstract_controller/callbacks.rb:17:in `process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications.rb:123:in `block in instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.15/lib/active_support/notifications.rb:123:in `instrument'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.15/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/abstract_controller/base.rb:121:in `process'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/abstract_controller/rendering.rb:45:in `process'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/test_case.rb:490:in `process'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/test_case.rb:54:in `process'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.15/lib/action_controller/test_case.rb:407:in `get'
# ./spec/controllers/pretty_search_controller_spec.rb:37:in `block (5 levels) in <top (required)>'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example.rb:114:in `instance_eval'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example.rb:114:in `block in run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/hooks.rb:104:in `call'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/hooks.rb:104:in `run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/hooks.rb:446:in `run_hook'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example.rb:111:in `run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:390:in `block in run_examples'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:386:in `map'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:386:in `run_examples'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:371:in `run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `block in run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `map'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `block in run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `map'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `block in run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `map'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/example_group.rb:372:in `run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/command_line.rb:28:in `map'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/command_line.rb:28:in `block in run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/reporter.rb:58:in `report'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/command_line.rb:25:in `run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/runner.rb:80:in `run'
# /home/shooma/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.14.6/lib/rspec/core/runner.rb:17:in `block in autorun'
我应该在哪个方向挖掘?
UPD:
在控制器调用render
时 - 我收到了错误。
但如果在控制器调用中render :text => 'foo'
- 测试通过。
虚拟应用程序配置中的问题。但我无法找到它。
UPD2:
下降规格:
context 'action should return @options and @results' do
it '@options should be an instance variable, which eq hash of three keys' do
get(:search, params)
# do something
end
end
控制器:
def search
# do something
render
end
解决方案:
这是我的错。问题是在测试环境中重载了一些内部rails类。抱歉花了很长时间,@ sameera207,谢谢您的参与。
答案 0 :(得分:2)
render
和redirect
之间的差异是,重定向加载重定向的操作但渲染没有,
例如:
def new
@user = User.new
end
def create
if <some conditions>
redirect_to 'new'
else
render 'new'
end
end
因此,当您致电render
时,它不会创建User.new
,只会重定向。所以你可能正在渲染一个从config
加载某些东西的动作。
所以解决方案就是
def new
@user = User.new
end
def create
if <some conditions>
redirect_to 'new'
else
@user = User.new
render 'new'
end
end
我希望你明白这个主意。如果你可以发布spec
某人可能会更容易帮助..