rspec and ruby 1.9.1: problem with dummy controller and routes

Posted by giorgian on Stack Overflow See other posts from Stack Overflow or by giorgian
Published on 2010-03-14T13:29:34Z Indexed on 2010/03/14 13:35 UTC
Read the original article Hit count: 281

Filed under:
|
|
|
|

I want to test a module that basically executes some verify statements, to ensure that actions are invoked with the correct method.

# /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

I tried this:

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

When testing:

$ 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
$ 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"

Is this a known issue? Is there a workaround?

© Stack Overflow or respective owner

Related posts about rspec

Related posts about ruby-on-rails