GET URL or path provided as string in rspec test

For the controller that determines the behavior of the sidebar, I need to call several URLs; the sidebar will change (ao) 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 to specify a url or path as a string? Is there a smarter way to set request. data request. instead of getting?

+8
get rspec
source share
2 answers

Is it possible to get the url or path as a string?

Not in the controller test. The test controller does not actually process the URL, it configures the test using one of the 5 supported request types (get, post, put, head, delete), and then calls the corresponding controller action. See the Rails Application Testing Guide .

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

+15
source share

Here is a workaround ... in case you wanted to do this ... not necessarily the best way, but the way ... it works on rails 3.2 ...

 module ActionController::TestCase::Behavior #just parse the url and then call the regular get method def get_path(path) parsed_params = Rails.application.routes.recognize_path path controller = parsed_params.delete(:controller) action = parsed_params.delete(:action) get(action, parsed_params) end end 
+7
source share

All Articles