Reputation: 14275
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?
Upvotes: 1
Views: 861
Reputation: 27779
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"
Upvotes: 1