使用curl来测试Rails的路线 [英] Using curl to test Rails routes
问题描述
我有我想要使用API更新模式叫用户。我想这样做的方法是创建一个更新的路由和code,做更新堵塞。我创建使用RSpec的测试,它似乎工作,但是,我想真正看到我的数据库中更改的数据,所以我试图用卷曲来更新我的开发数据库。
I have a model called users that I want to update using an API. The way I thought to do this is by creating an update route and plugging in code that does the updating. I created a test using RSpec and it seemed to work, however, I want to actually see the changed data in my database so I was trying to use curl to update my development database.
我试过命令
curl -X PUT -d "attribute1 = value1, attribute2 = value2" http://localhost:3000/users/1
但我得到了一个错误
but I got an error
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Action Controller: Exception caught</title>
<style>
.
.
.
</style>
</head>
<body>
<h1>
NoMethodError
in UsersController#update
</h1>
<pre>You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]</pre>
.
.
.
继承人的相关线路在routes.rb中
Heres's the relevant line in the routes.rb
resources :users, :only => [:create, :update, :destroy]
下面是有关的code控制器
Here's the relevant code in the controller
def update
@user = User.find(params[:id])
if params[:user][:attribute1] == ("x" || "y" || "z")
params[:user][:attribute3] = Time.now
end
@user.update_attributes(:user)
render :nothing => true
end
最后下面是通过规范code
And finally here's the spec code that passed
describe "PUT 'update'" do
before(:each) do
@user = Factory(:user)
end
describe "success" do
it "should be successful" do
put :update, :id => @user, :user => { :attribute1 => "x", :attribute2 => "xyz"}
response.should be_success
end
it "should update attribute" do
old_attribute = @user.attribute3
put :update, :id => @user, :user => { :attribute1 => "x", :attribute2 => "xyz"}
@user.attribute3 != old_attribute
end
end
end
我试图找到一个很好的卷曲教程来检查我的语法,但我只能找到的例子,所以我也不太清楚,如果语法是正确的。
I tried to find a good curl tutorial to check my syntax but i could only find examples, so I'm not too sure if the syntax is right.
推荐答案
您 PUT
数据应该是这样的:
用户[ATTRIBUTE1] =值1&放大器;用户[attribute2] =值2
为你构建一个URL的查询字符串参数的方式类似。
Similar to the way you'd construct parameters on the query string of a URL.
这篇关于使用curl来测试Rails的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!