I have a Rails RESTful web service application that accepts a value from a client to increment the value in the database. The database value is an integer, but when using rspec to test the code, the value being passed in is interpreted as a string.
I’m using Rails 3.1 and Ruby 1.9.2.
Here’s the rspec snippet:
...
it "should find Points and return object" do
put :update, :username => "tester", :newpoints => [10, 15, 0], :format => :xml
end
...
Here’s the controller code:
...
respond_to do |format|
if points.update_attributes([xp + :newpoints[0]][sp + :newpoints[1]][cash + :newpoints[2]])
format.json { head :ok }
format.xml { head :ok }
...
xp, sp and cash are values from the database and have been validated as Fixnum datatype. The error I am getting is:
TypeError: String can't be coerced into Fixnum
How do I write my test to ensure that the parameters being passed are passed as the proper datatype?
I can include more of the code if needed. Thanks in advance!
It took me a bit of head banging, but I found out that I was passing everything in wrong. The solution I’ve come up with is definitely not the best solution and could probably be re-written, but it works and for now, that’s sufficient.
The change to the rspec snippet was to create hash represented by symbol :newpoints
The handling of this request in the controller required a bit of tweaking, but here’s the pertinent pieces:
Obviously, much can be done to make this follow DRY and what not, so any suggestions are still welcome. Thanks in advance!