Rails 3.1 Web 服务的数据类型未正确传递
我有一个 Rails RESTful Web 服务应用程序,它接受来自客户端的值以增加数据库中的值。数据库值是一个整数,但是当使用 rspec 测试代码时,传入的值被解释为字符串。
我正在使用 Rails 3.1 和 Ruby 1.9.2。
这是 rspec 片段:
...
it "should find Points and return object" do
put :update, :username => "tester", :newpoints => [10, 15, 0], :format => :xml
end
...
这是控制器代码:
...
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 和 cash 是来自数据库的值,并且已被验证为 Fixnum 数据类型。我收到的错误是:
TypeError: String can't be coerced into Fixnum
如何编写测试以确保传递的参数作为正确的数据类型传递?
如果需要,我可以包含更多代码。提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我花了一番心思,但我发现我传递的所有内容都是错误的。我提出的解决方案绝对不是最好的解决方案,可能可以重写,但它有效,目前就足够了。
对 rspec 片段的更改是创建由符号 :newpoints 表示的散列
在控制器中处理此请求需要进行一些调整,但以下是相关部分:
显然,可以做很多事情来使其遵循 DRY 以及其他方法,所以仍然欢迎任何建议。提前致谢!
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!