Test Ruby-on-Rails controller with RSpec and different route name

Posted by jhwist on Stack Overflow See other posts from Stack Overflow or by jhwist
Published on 2010-03-25T14:30:08Z Indexed on 2010/03/25 14:33 UTC
Read the original article Hit count: 504

Filed under:
|

I have a Rails model named Xpmodule with a corresponding controller XpmoduleController.

class XpmoduleController < ApplicationController
  def index
    @xpmodule = Xpmodule.find(params[:module_id])
  end

  def subscribe
    flash[:notice] = "You are now subscribed to #{params[:subscription][:title]}"
    redirect_to :action => :index
  end
end

The original intent was to name the model Module which for obvious reasons doesn't work. However I still want to have the URLs look like /module/4711/ therefore I added this to my routes.rb:

map.connect '/module/:module_id', :controller => 'xpmodule', :action => 'index'

map.connect '/module/:module_id/subscribe', :controller => 'xpmodule',
  :action => 'subscribe'

Now I want to test this controller with Rspec:

describe XpmoduleController do
  fixtures :xpmodules
  context "index" do
    it "should assign the current xpmodule" do
      xpm = mock_model(Xpmodule)
      Xpmodule.should_receive(:find).and_return(xpm)
      get "index"
      assigns[:xpmodule].should be_an_instance_of(Xpmodule)
    end
  end
end

for which I get No route matches {:action=>"index", :controller=>"xpmodule"}. Which of course is sort-of right, but I don't want to add this route just for testing purposes. Is there a way to tell Rspec to call a different URL in get?

© Stack Overflow or respective owner

Related posts about rspec

Related posts about ruby-on-rails