Testing GET in a rails controller - ruby-on-rails

I have a feeling I'm just doing something wrong syntactically but it's surprisingly difficult to Google "GET" so I was hoping someone here might know the answer to this.
I'm trying to test a Rails controller from an RSpec test. I'm following an example I found here - http://www.elevatedrails.com/articles/2007/09/10/testing-controllers-with-rspec/ but I'm stuck on the actual execution of the method I'm testing.
I'm doing a GET request where as the post above does a POST. I'm passing in 2 parameters manufacturer and model.
My URL will ideally look something like http://mysite.com/Products/index/Manufacturer/ModelName
I can't figure out the syntax for the get request call in the rest. Right now I have
get :index, :manufacturer=>#manufacturer, :modelName=>#modelName
and I get back
ArgumentError in 'ProductController Find a valid product should retrieve the product'
wrong number of arguments (0 for 2)
Any thoughts?
edit: It should be noted #manufacturer and #modelName are defined in before(:each)

As i suspected this was me being green to rails programming.
I was defining the controller method as
def index(manufacturer, modelName)
When really i needed to use the params hash to access the attributes. I then had to define a custom route as id is the only parameter expected to be passed into a controller method by default.
once i did that i changed the spec to read
get :index, {:manufacturer=>#manufacturer, :modelName=>#modelName}
and it worked.
Thanks for the comments everyone.

Related

Rails 5.1 Route Constraint Not Passing Params

Im having an issue after updating to Rails 5.1 with a route constraint.
Basically my route is matching, but no params are being passed through to the controller action. This code has been working for a while, but now it doesn't and im kind of stuck as to why.
Hitting the URL
localhost:3000/?edd_action=activate_license&item_name=ProductA&license=123
Route
get '/' => 'api/v1/legacy_licenses#activate', constraints: { query_string: /edd_action=activate_license/ }
My action code looks like
def load_license_key
if params[:license]
...
end
end
The problem is the params hash never picks up the license param, or the item name one for that matter.
If I inspect the request.query_string on that action all I see is "edd_action=activate_license" so I don't know where the rest of the params are going.
Can anyone help? Changing the URL params isn't an option as its only like this to integrate with legacy software.

Don't understand when a ROR View will/will not have a param for a model

I am having difficulty understanding how to cause a View in ROR4 to have a param for a model. It shows up when I try to use strong parameters in a controller, specifically:
def model1_params
params.require(:model1).permit(:attr1,attr2)
end
Sometimes it works. Sometimes I get "param not found: model1"
My understanding is that it fails (when it does) because the web page being submitted doesn't have any param called "model1" and that it works (when it does) because something I have done has caused the web page submission to have this param. In the latter case, the param is a hash representing the fields of the model, with members for attr1, attr2, etc.
Does it work when I use "form_for" (which specifies a model) but not the other kind of form call, which is not tied to a model?
Note: I am writing because I've had this problem for several weeks with no progress. I have searched the Rails doc and lots of instructional examples on the web but with no luck. I know I'm being dense, but so far I've not found a real API reference document, nor a real API programmer's guide that covers strong parameters.
I've found lots of examples on strong parameters but none of them clearly explain how it works, that would let me figure out on my own what I am doing wrong.
Say your controller's method is associated to a route model1_controller_method_path.
Your method can be called from a link in a view, and if the model1 parameter is not present, yes, you will get a 'param not found' error. To avoid this, make sure to have your parameter passed from your link_to as in:
link_to model1_controller_method_path(:model1 => the_value_you_want)

Dynamic method call in routes spec

I am testing simple get requests for my routes using rspec in my Rails 3.2 application. Since all are get requests, and all just have different action names which are similar to the views' names, it would be really repetitive to manually write a different test for each get request.
Instead, I wanted to come up with something like this:
%(action_1 action_2 action_3 action_4).each do |action|
it "routes to the #{action} page" do
get("liver_diseases#{action}_path").should route_to("liver_diseases##{action}")
end
end
It fails at this pseudocode: get("liver_diseases_#{action}_path")
So what I need to do is a dynamic method call - but for what I have found out, that would involve .send(:method_name), for which I need to know the class name. And I couldn't find that.
What do I need to do for this method call to work?
that would involve .send(:method_name), for which I need to know the
class name
When the receiver is missing, it's always self. In the context of a controller example, self should be a controller instance. So you should be able to get that path with:
send "liver_diseases_#{action}_path"
which should be equivalent to:
controller.send "liver_diseases_#{action}_path"

Rails 3 gives routing error with point in URL

I have a search form written with Rails 3 when I query it everything works fine as long as I do not put a point in my query. Eg:
http://localhost:3000/en/job/search/q/test - WORKS
http://localhost:3000/en/job/search/q/test. - DOES NOT WORK
URL with point at the end gives a
Routing Error: No route matches [GET] "/en/job/search/q/test.
Does anybody know how I can solve this? Thanks.
By default, Rails interprets everything to the right of the decimal as the format. You need to set the :constraints
Here is a good article on the subject: http://coding-journal.com/rails-3-routing-parameters-with-dots/
Here is the reference in the Rails API that should help you resolve your issue:
http://guides.rubyonrails.org/routing.html#specifying-constraints
http://guides.rubyonrails.org/routing.html#dynamic-segments
Since your passing a string in the search as a get request, you might also consider route globbing: http://guides.rubyonrails.org/routing.html#route-globbing
Your route would be something like this:
match ":language/job/search/*query"
and in your controller, you would get the value from the route using the params[] array:
q = params[:query]
Be sure to use best practices when passing this to ActiveRecord to avoid a SQL injection attack.
What #iltempo said.
Also, it would be a good idea to switch your search from using GET requests to POST requests to make all these problems go away.

Raw POST from Ruby on Rails

I'm trying to do a raw POST to an internal page from a unit test, and for some reason I'm getting the following error:
NoMethodError: undefined method `symbolize_keys' for #<String:0x258a910>
unit/call_route_attempt_test.rb:10:in `test_response_from_call_info'
My code looks like this:
post :call_info, '<?xml version="1.0" encoding="UTF-8"?><request-call-info>...SOME XML HERE...</request-call-info>'
That should be fine according to the API docs. The only format I can get to work is if I send it as parameters like so:
post :call_info, :post => {:a_var => '<?xml version="1.0" encoding="UTF-8"?><request-call-info>...SOME XML HERE...</request-call-info>'}
I have a feeling I need to require some part of the library that for some reason isn't working here.
Edit
As user Sarah Mei points out, I'm calling the wrong post method. I think it has to do with my class inheritance, which I'm not sure I can change given the circumstances of me running a test. The class definition is as follows:
class anApiControllerTest < ActionController::TestCase
The method I'm trying to implement is the post (ActiveResource::Connection) method listed on the Rails API docs. I'm really new to this, quite lost and on a deadline. Any help is greatly appreciated. Thanks!
It depends on which post method you're calling. There's one in ActionController::Integration::Session, one in ActionController::TestProcess, one in ActiveResource::Connection...etc. Most of them take a params hash as the second parameter, so if the second call works, you're probably not calling the post you think you're calling.

Resources