在 Rails 3 项目中生成 rspec:install 后,任何新的脚手架都将包含一些默认规范。我对 get、post、put 和 delete 方法以及它们实际被调用的内容感到困惑?
具体来说,在这个例子中,行 delete :destroy, :id => "1"
被调用到底是什么? Controller ?但 Controller 没有“删除”方法...尽管它有 destroy
。但对其调用“delete”不会执行任何操作,因此将 :destroy
作为参数传递是没有意义的......这是如何工作的?
这是为 resources_controller 生成的规范的一部分。我已经遗漏了,但是 put :update
和 post :create
和 get :edit
, :show 也存在同样的情况
、:new
& :索引
#app/controllers/resources_controller.rb
describe ResourcesController do
def mock_resource(stubs={})
@mock_resource ||= mock_model(Resource, stubs).as_null_object
end
...
describe "DELETE destroy" do
it "destroys the requested resource" do
Resource.stub(:find).with("37") { mock_resource }
mock_resource.should_receive(:destroy)
delete :destroy, :id => "37"
end
it "redirects to the resources list" do
Resource.stub(:find) { mock_resource }
delete :destroy, :id => "1"
response.should redirect_to(resources_url)
end
end
end
最佳答案
get
、post
、put
和 delete
是请求中使用的 HTTP 动词。请参阅:http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
是的,以下参数是在 Controller 上调用的操作,:update
、:create
等。
关于ruby-on-rails - 带有 CRUD 方法调用的 RSpec 代码示例基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5024763/