2011-02-17 21 views
2

在Rails 3項目中生成rspec:install後,任何新的腳手架都將包含一些默認規範。我很困惑得到,發佈,把&刪除方法和他們實際上被調用?帶有CRUD方法調用的RSpec代碼示例基礎知識

具體來說,在這個例子中,行delete :destroy, :id => "1"正在調用什麼?控制器?但是控制器沒有「刪除」方法......雖然它的確有destroy。但調用'刪除'它不應該做任何事情,所以傳遞:destroy作爲一個參數是沒有意義的......這是如何工作的?

下面是resources_controller生成規範的一部分。我已經離開了,但存在同樣的事情put :updatepost :createget :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 

回答

2

getpostputdelete都在請求中使用的HTTP動詞。請參閱:http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

是的,下面的說法是你的控制器,:update:create

+0

我明白HTTP動詞...我不明白的是他們是如何工作的...在這裏...在這個測試中?你說「get」是Rails(或Ruby?)中的保留字,我可以在任何地方使用它嗎?可能不會。這是一個* some * object的方法...在某處...但是在哪裏?或者是什麼? – Meltemi 2011-02-17 05:14:14

+0

在這個例子中,RSpec使用軌道附帶的內置動作控制器測試方法。如果您有興趣,請查看這些方法的源代碼https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/test_case.rb#L353 – raidfive 2011-02-17 05:24:00