开发者

RSpec code example basics with CRUD method calls

开发者 https://www.devze.com 2023-02-11 04:01 出处:网络
After generate rspec:install in a Rails 3 project any new scaffolding will include some default specs. I\'m confused about the get, post, put & delete methods and what they\'re actually being call

After generate rspec:install in a Rails 3 project any new scaffolding will include some default specs. I'm confused about the get, post, put & delete methods and what they're actually being called on?

specifically, in this example, the line delete :destroy, :id => "1" is being called on what exactly? the controller? but the controller doesn开发者_StackOverflow't have a 'delete' method...though it does have destroy. but calling 'delete' on it shouldn't do anything so passing :destroy as an argument is meaningless... how does this work?

Here's a portion of the generated specs for a resources_controller. I've left out, but the same thing exists for put :update and post :create and get :edit, :show, :new & :index

#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, and delete are the HTTP verbs used in the request. See: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

And yes, the following argument is the action being called on your controller, :update, :create, etc.


When you write controller specs, RSpec includes the ControllerExampleGroup module, which "extends ActionController::TestCase::Behavior to work with RSpec.".

ActionController::TestCase::Behavior is where these methods are defined.

0

精彩评论

暂无评论...
验证码 换一张
取 消