Note * I’m using Factory_girl, not sure if that matters here
Currently, I do the following to prep for doing a post:
p = Factory.build(:le_object, :template_id => t.id.to_s)
Notice that I am deliberately converting it to a string
And then I just do your ordinary post:
post :create, :le_object => p.attributes
But, what is fishy, is when I look in the test log, I see this:
Parameters: {"action"=>"create", ... "template_id"=>0, ... }
Notice how the id is NOT a string.
Now, this messes up the controller, because I do a not .empty? check on that field (because it is optional), and then that method in the controller does something entirely different.
This works fine in the app, when it is actually running, as for the same method, the console outputs this in dev / production:
Parameters: {"action"=>"create", ... "template_id"=>"1"...}
Notice how here the 1 is a string, because it came from a form, and in HTTP, everything is strings.
This is what in my code is failing, specifically:
if not params[:le_object][:template_id].nil? and not params[:le_object][:template_id].empty?
The issue currently, is that in the test, because it’s passing a number, instead of the string representation, I’m getting the error undefined method empty? for #:Fixnum
So… How do I change my code (test or otherwise) such that I can get this to work?
Rails defines the
.blank?method onObject, meaning it works on just about anything (strings, numbers, etc.) The definition of.blank?is very simple:It will default to use
empty?, but if that method doesn’t exist on the object, it will just call!self. If the item isnil, then!nilwill evaluate totrue.