3

この疑問の冗談はあらかじめお詫び申し上げます。あなたが私に同行すれば、実際には非常にシンプルだと思うでしょう...私の限られたRailsドメインの知識があれば、説明するのは難しいです。 Railsを使った作業とテストActionControllerのrespond_with

an ActionController commit dated Aug 6でこのコメントを考える:私は respond_withを使用することは非常に基本的なコントローラを変更した

=== Builtin HTTP verb semantics 

Rails default renderer holds semantics for each HTTP verb. Depending on the 
content type, verb and the resource status, it will behave differently. 

Using Rails default renderer, a POST request for creating an object could 
be written as: 

    def create 
    @user = User.new(params[:user])  
    flash[:notice] = 'User was successfully created.' if @user.save 
    respond_with(@user) 
     end 

Which is exactly the same as: 

    def create 
    @user = User.new(params[:user]) 

    respond_to do |format| 
     if @user.save 
     flash[:notice] = 'User was successfully created.' 
     format.html { redirect_to(@user) } 
     format.xml { render :xml => @user, :status => :created, :location => @user } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @user.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

The same happens for PUT and DELETE requests. 

。 Railsの自動生成テストで空を渡そうとすると、2つの仕様が失敗する以外はすべてうまくいくように見えるparams &メソッドを更新する。私はこの動作を単純にsave/elseで修正できますが、この "新しい"機能を理解しようとしています。私は、デフォルトの仕様が古くなった形で書かれている可能性があると思います。

コメントをコミットから:「Since the request is a POST, respond_with will check wether @people resource have errors or not. If it has errors, it will render the error object with unprocessable entity status (422).は」

だから私は、それが(処理不能エンティティの状態」をテストするように再書き込みすることができた(下記)POSTの下で最後のテスト/スペックにスクロールダウン422) "と通過して、すべてがpeachy-keenですか?

マイコントローラー:

class ClownsController < ApplicationController 
    respond_to :html, :json 

    def index 
    respond_with(@clowns = Clown.all) 
    end 

    def show 
    respond_with(@clown = Clown.find(params[:id])) 
    end 

    def new 
    respond_with(@clown = Clown.new) 
    end 

    def edit 
    respond_with(@clown = Clown.find(params[:id])) 
    end 

    def create 
    @clown = Clown.new(params[:clown]) 
    flash[:notice] = 'Clown was successfully created.' if @clown.save 
    respond_with(@clown) 
    end 

    # Replacing def create above with this won't Fail the speC## 
    # 
    # def create 
    # @clown = Clown.new(params[:clown]) 
    # respond_with(@clown) do |format| 
    #  if @clown.save 
    #  flash[:notice] = 'Clown was successfully created.' 
    #  format.html { redirect_to @clown } 
    #  else 
    #  format.html { render :action => :new } 
    #  end 
    # end 
    # end 


    def update 
    @clown = Clown.find(params[:id]) 
    flash[:notice] = 'Clown has been updated.' if @clown.update_attributes(params[:clown]) 
    respond_with(@clown) 
    end 

    def destroy 
    @clown = Clown.find(params[:id]) 
    flash[:notice] = 'Successfully deleted clown.' if @clown.destroy 
    respond_with(@clown) 
    end 
end 

テストスペック:

$ rspec spec/ 
.......F....F.............. 

Failures: 

    1) ClownsController POST create with invalid params re-renders the 'new' template 
    Failure/Error: response.should render_template("new") 
    expecting <"new"> but rendering with <"">. 
    Expected block to return true value. 
    # (eval):2:in `assert_block' 
    # ./spec/controllers/clowns_controller_spec.rb:69:in `block (4 levels) in <top (required)>' 

    2) ClownsController PUT update with invalid params re-renders the 'edit' template 
    Failure/Error: response.should render_template("edit") 
    expecting <"edit"> but rendering with <"">. 
    Expected block to return true value. 
    # (eval):2:in `assert_block' 
    # ./spec/controllers/clowns_controller_spec.rb:107:in `block (4 levels) in <top (required)>' 

ここではclowns_controller_spec.rbの一部です:

require 'spec_helper' 

describe ClownsController do 

    def mock_clown(stubs={}) 
    (@mock_clown ||= mock_model(Clown).as_null_object).tap do |clown| 
     clown.stub(stubs) unless stubs.empty? 
    end 
    end 

... 

    describe "POST create" do 

    describe "with invalid params" do  
     it "re-renders the 'new' template" do 
     Clown.stub(:new) { mock_clown(:save => false) } 
     post :create, :clown => {} 
     response.should render_template("new") 
     end 
    end 
+0

この問題を再訪しても何が起きているのかまだ分かりません...問題テストをスキップしています...明らかに理想的ではありません。 – Meltemi

+0

https://github.com/rspec/rspec-rails/issues/103 – scragz

答えて

2

は、次の

であなたのピエロのクラスをからかってみ
Clown.stub(:new) { mock_clown(:errors => {:any => 'error'}) } 

このように、respond_withメソッドはモデルの保存が失敗したことを知り、新しいテンプレートをレンダリングします。

関連する問題