Reputation: 41
I am able to successfully call actions in my engine controllers from my ActionController::TestCase derived test with:
test "should get new" do
get :new, {'form_id' => '1-2', use_route: :engine_controllers}
assert_response :success
assert_match(/Hello World/, @response.body)
end
But I am getting an error when referencing a routing method within the controller or view. Specifically, from the following routes:
Routes for FormEngine::Engine:
GET /:form_id(.:format) form_engine/submissions#new
submissions POST /:form_id(.:format) form_engine/submissions#create
POST /payment_info/:id(.:format) form_engine/submissions#update
submission GET /submissions/:id(.:format) form_engine/submissions#show
These are intentionally not restful routes.
When I reference submissions_path
or form_engine.submissions_path
I get the following error in my test run:
ActionController::UrlGenerationError: No route matches {:controller=>"form_engine/submissions", :action=>"create"} missing required keys: [:form_id]
I do not get this error when running the server. I get the path expected: /form_engine/1-2
I have traced the problem down to the fact that when run as a server request.path_parameters
has the :form_id
in it, but in the test case run request.path_parameters
is empty.
In a non-engine controller test case, request.path_parameters
correctly has the path parameters. From what I can tell, in
actionpack (4.0.2) lib/action_controller/test_case.rb:189:
extra_keys = routes.extra_keys(parameters)
ends up listing all of the parameters as extra keys and get added to non_path_parameters. I have not figured out what is misfiring in routes.extra_keys so that :form_id is being listed as an extra key.
I'm guessing that routes is losing the use_route
or something like that.
I found a somewhat similar question, but I'm not sure it is actually related: Rails 3.2 Engines - routes not working in test cases. So far there hasn't been a satisfactory answer to it.
If anyone can see an obvious mistake I am making or knows of a workaround, I would love to hear it.
Thanks in advance.
Upvotes: 4
Views: 1003
Reputation: 87
I found the solution (for me) in the Ruby on Rails Engine Guide
module MyEngineName
class FooControllerTest < ActionController::TestCase
setup do
@routes = Engine.routes
end
def test_index
get :index
...
end
end
end
Upvotes: 3
Reputation: 10763
I don't know if this would have helped you, but the solution for me (Rails 4.1.8) seemed to have been use_route
, as you mentioned:
# my_controller_test.rb
module MyModule
class BlahControllerTest < ActionController::TestCase
test "gets index" do
get :index, use_route: 'my_module'
end
end
end
From:
Multiple Rails engine rspec controller test not work
Much happy.
Upvotes: 0