开发者

With RSpec2 and Rails3.0.4, how to test your generic controller like admin_controller.rb from XML

开发者 https://www.devze.com 2023-02-12 11:20 出处:网络
What\'s the problem I wanna test a before filter that render errors in XML and JSON if failing. When testing it from a classic controller it works fine, when testing from

What's the problem

I wanna test a before filter that render errors in XML and JSON if failing. When testing it from a classic controller it works fine, when testing from the ApplicationController directly it fails.

A light version of the code

specs/controller/admin_controller_spec.rb

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe AdminController do
  controller(AdminController) do
    def index
      render :text => 'ok'
    end
  end

  it "should include errors properly formated" do
    get :index, :format => 'xml'
    response.body.should == "block"
  end
end

app/controller/admin_controller.rb

class AdminController < ApplicationController
  before_filter :block_something

  protected

    def block_something
      respond_to do |wants|
        wants.xml {
          render :text => 'block' 
        }
      end
    end
end

Error from tests

Failure/Error: get :index, :format => format
  TypeError:
    can't convert nil into String

Update, with backtrace : thanks Ryan Bigg for the recommendation

It gives different controller and filter names than the example above. The app use admin_controller.rb and block_suspended filter.

 # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/template/resolver.rb:61:in `<<'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/template/resolver.rb:61:in `build_path'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/template/resolver.rb:54:in `find_templates'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/template/resolver.rb:20:in `find_all'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/template/resolver.rb:39:in `cached'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/template/resolver.rb:19:in `find_all'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/paths.rb:21:in `find_all'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/paths.rb:20:in `each'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/paths.rb:20:in `find_all'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/paths.rb:21:in `find_all'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/paths.rb:20:in `each'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/paths.rb:20:in `find_all'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/paths.rb:28:in `exists?'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_view/lookup_context.rb:90:in `template_exists?'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/view_paths.rb:11:in `__send__'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/view_paths.rb:11:in `template_exists?'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/layouts.rb:272:in `_layout'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/layouts.rb:345:in `_default_layout'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/layouts.rb:323:in `_layout_for_option'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/layouts.rb:290:in `_normalize_options'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/rendering.rb:41:in `_normalize_options'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/compatibility.rb:50:in `_normalize_options'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/rendering.rb:101:in `render_to_string'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/rendering.rb:93:in `render'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/rendering.rb:17:in `render'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:40:in `render_without_wicked_pdf'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/core_ext/benchmark.rb:5:in `ms'
     # /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/core_ext/benchmark.rb:5:in `ms'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:40:in `render_without_wicked_pdf'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
     # /Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:39:in `render_without_wicked_pdf'
     # ./app/controllers/admin_controller.rb:36:in `block_suspended'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/mime_responds.rb:192:in `call'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/mime_responds.rb:192:in `respond_to'
     # ./app/controllers/admin_controller.rb:33:in `block_suspended'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:466:in `_run__1727617933__process_action__199225275__callbacks'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:409:in `send'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:93:in `send'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:93:in `run_callbacks'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/callbacks.rb:17:in `process_action'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/notifications.rb:52:in `instrument'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
     # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/notifications.rb:52:in `instrument'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
     # /Library/Ruby/开发者_运维百科Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/rescue.rb:17:in `process_action'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/base.rb:119:in `process'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/rendering.rb:41:in `process'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/testing.rb:12:in `process_with_new_base_test'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/test_case.rb:412:in `process'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/test_case.rb:47:in `process'
     # /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/test_case.rb:350:in `get'
     # ./spec/controllers/admin_controller_spec.rb:272
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `instance_eval'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:29:in `run_in'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `run_all'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `each'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:64:in `run_all'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/hooks.rb:110:in `run_hook'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `eval_before_eachs'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `each'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:191:in `eval_before_eachs'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:144:in `run_before_each'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:48:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:106:in `with_around_hooks'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:46:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:99:in `with_pending_capture'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `catch'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:98:in `with_pending_capture'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example.rb:45:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:262:in `run_examples'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `map'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:258:in `run_examples'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:232:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `map'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `map'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `map'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `map'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/example_group.rb:233:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `map'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:27:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/reporter.rb:12:in `report'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:24:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
     # /Library/Ruby/Gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `autorun'
     # /usr/bin/rspec:19

Any thought on why this appends? If not the right way to test it, how to test such behavior?


I've now upgraded to ruby 1.9.2 and all specs passes without any changes required.

Looks like a limitation of Ruby 1.8.7 that would need extra workarounds.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号