Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET an url or path, provided as string, in an rspec test

Tags:

get

rspec

For a controller that determines the behaviour of a sidebar, I need to call several urls; the sidebar will change (a.o) based on the page on which it appears.

describe SidebarController do
  before(:each) do
    @sidebar = SidebarController.new
  end
  it 'should return :jobseekers for root_path' do
    get(root_url)
    @sidebar.section(root_path).should eq :jobseekers
  end
end

This, however, fails with ActionController::RoutingError: No route matches {:controller=>"sidebar", :action=>"http://test.host/"}

Can I get an url or path as string? Is there a smarter way to set the request. data, rather then GETting it?

like image 525
berkes Avatar asked Jul 12 '11 14:07

berkes


1 Answers

Can I get an url or path as string?

Not in a controller test. The test controller is not actually processing a URL, it is setting up the test using one of the 5 supporting request types (get, post, put, head, delete) and then calling the appropriate controller action. See the Guide to Testing Rails Applications.

What you are looking for is an integration test, or a "request spec" in RSpec terms.

like image 136
zetetic Avatar answered Oct 18 '22 21:10

zetetic