1

「Project」というモデルの下にネストされたリソースである「Post」というモデルがあり、コントローラーをテストしようとしています。コードはブラウザで動作しますが、テストでは動作しません。これがテストです

context "on POST to :edit" do
  setup do
    post( :edit,
      :project_id => @project1.to_param, 
      :id => @post1.to_param, 
      :post => { :title => 'new title', :text => 'other text' } 
    )
  end
  should_assign_to :post
  should_assign_to :project
  should_respond_with :success

  should "update post values" do
    assert_equal 'other text', assigns['post'].text
  end

私がこれをどのように台無しにしているのか分かりますか?

4

1 に答える 1

0

これは、私が Rails の REST アーキテクチャーまたは投稿構文を理解していなかった結果です。POST の代わりに PUT を使用する必要があり、呼び出しは次のようになります。

context "on PUT to :update" do
  setup do
    put :update, { 
      :project_id => @project1.to_param, 
      :id => @post1.to_param, 
      :post => { :title => 'new title', :text => 'other text' } 
    } 
  end

  should_assign_to :post
  should_assign_to :project
  should_respond_with :success

  should "update post values" do
    assert_equal 'new title', assigns['post'].title
    assert_equal 'other text', assigns['post'].text
  end
end

何らかの理由でネストされた ID を正しく処理していたため、間違った構文を使用していました。

于 2010-01-12T01:55:21.787 に答える