[rspec-users] [rails] mock controllers, different behaviour between ruby 1.8.7 and ruby 1.9.1

David Chelimsky dchelimsky at gmail.com
Mon Mar 15 08:44:05 EDT 2010


On Mon, Mar 15, 2010 at 3:48 AM, giorgian <giorgian at gmail.com> wrote:
> Hi,
>
> I wanted to test something like this:
>
> # /lib/rest_verification.rb
> module RestVerification
>  def self.included(base) # :nodoc:
>    base.extend(ClassMethods)
>  end
>
>  module ClassMethods
>    def verify_rest_actions
>      verify :method => :post, :only => [:create], :redirect_to  =>
> { :action => :new }
>      ...
>    end
>  end
> end
>
> (just send some verify to controller).
>
> My attempt:
>
> describe RestVerification do
>
>  class FooController < ActionController::Base
>    include RestVerification
>    verify_rest_actions
>
>    def new ; end
>    def index ; end
>    def create ; end
>    def edit ; end
>    def update ; end
>    def destroy ; end
>  end
>
>  # controller_name 'foo' # this only works with ruby 1.8.7 : 1.9.1
> says "uninitialized constant FooController"
>  tests FooController # this works with both
>
>  before(:each) do
>    ActionController::Routing::Routes.draw do |map|
>      map.resources :foo
>    end
>  end
>
>  after(:each) do
>    ActionController::Routing::Routes.reload!
>  end
>
>  it ':create should redirect to :new if invoked with wrong verb' do
>    [:get, :put, :delete].each do |verb|
>      send verb, :create
>      response.should redirect_to(new_foo_url)
>    end
>  end
>
>  ...
> end
>
>
> Using ruby 1.8.7 it works:
>
> $ ruby -v
> ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
> $ rake
> RestVerification
>  :create should redirect to :new if invoked with wrong verb
>
> Finished in 0.175586 seconds
>
> Using ruby 1.9.1 it fails:
>
> $ rvm use 1.9.1
> Using ruby 1.9.1 p378
> $ rake
> RestVerification
>  :create should redirect to :new if invoked with wrong verb (FAILED -
> 1)
> 1)
> 'RestVerification :create should redirect to :new if invoked with
> wrong verb' FAILED
> expected redirect to "http://test.host/foo/new", got redirect to
> "http://test.host/spec/rails/example/controller_example_group/
> subclass_1/foo/new"
>
>
> What can I do to make it work under ruby 1.9.1?

This has to do with scoping changes in ruby 1.9.1, which I hear are
going to be changed back in 1.9.2, but that's only hearsay.

Try defining FooController outside the describe block. Either that or
prefixing it with :: like this:

  class ::FooController < ::ActionController::Base

Either should solve the problem.

HTH,
David


More information about the rspec-users mailing list