2010-10-14 76 views
0

我正在嘗試控制器上的以下功能測試。我使用以下爲什麼rspec重定向在測試期間沒有獲得完整路線?

  • RSpec2
  • 的Rails 3
  • Shoudla
  • 摩卡

這裏的測試

context "POST on create should be successful" do 
    before(:each) do 
    Model.any_instance.expects(:save).returns(true).once 
    Model.any_instance.stubs(:id).returns(1) 
    post :create, :model => {} 
    end 

    it { should assign_to(:model).with_kind_of(Model) } 
    it { should set_the_flash.to("Model was created successfully.")} 
    it { should redirect_to(model_path(1))} 
end 

和被測控制器

def create 
    @model = Model.new(params[:model]) 

    if @model.save 
    flash[:success] = "Model was created successfully." 
    end 
    respond_with @model 
end 

唯一失敗的測試是第三次測試,指出,要返回的路徑是「http://test.host/models」,而不是「http://test.host/models/1」

當我在瀏覽器中運行應用程序時,我得到了正確的重定向。

+0

我離開這個問題開放,我結束瞭解決這個問題,通過不使用模擬行爲,只使用工廠調用來獲得所需的行爲。所以如果有人能解釋爲什麼模擬不正確,請告知。 – 2010-10-19 17:11:19

回答

0

我想你也需要模擬to_params。它是由路線系統使用

context "POST on create should be successful" do 
    before(:each) do 
    Model.any_instance.expects(:save).returns(true).once 
    Model.any_instance.stubs(:id).returns(1) 
    Model.any_instance.stubs(:to_params).returns(1) 
    post :create, :model => {} 
    end 

    it { should assign_to(:model).with_kind_of(Model) } 
    it { should set_the_flash.to("Model was created successfully.")} 
    it { should redirect_to(model_path(1))} 
end 
+0

我仍然得到與額外的存根相同的結果。我試圖覆蓋'to_param'和'to_params'。我認爲你的意思是'to_param'來覆蓋。 – 2010-10-18 12:44:57

相關問題