0

次のエラーを作成するテストがあります。

1) Failure:
test_should_get_create(ProductRequestsControllerTest) [/Users/noahc/Dropbox/mavens/test/functional/product_requests_controller_test.rb:37]:
"ProductRequest.count" didn't change by 1.
<2> expected but was
<1>.

これをトラブルシューティングするにはどうすればよいですか?具体的には、どうすればより具体的な詳細なエラーを取得できますか?

これが私のテストです:

 test "should get create" do
    sign_in(FactoryGirl.create(:user))
     assert_difference('ProductRequest.count') do
      post :create, product_request: FactoryGirl.attributes_for(:product_request)
    end

    assert_response :success
  end

これが私のコントローラーです:

 def create
  cart = current_cart
  rows = CartRow.find_all_by_cart_id(cart.id)

  rows.each do |row|
    product_request = ProductRequest.new(params[:product_request])
    product_request.user_id = current_user.id
    product_request.product_id = row.product_id
    product_request.quantity = row.quantity
    product_request.save
  end

  redirect_to root_path 
 end

問題は、カートが定義されていないことだと思います。unit :: testが表示できるカートを作成するにはどうすればよいですか?FactoryGirlを使用してカートを作成しようとしましたが、うまくいかなかったようです。

carts_factory.rb

FactoryGirl.define do

  factory :cart do
  end
end

更新されたテスト:

test "should get create" do
    sign_in(FactoryGirl.create(:user))
    user = FactoryGirl.create(:user)
    product = FactoryGirl.create(:product)
     assert_difference('ProductRequest.count') do
      post :create, product_request: FactoryGirl.attributes_for(:product_request, user: user.id, product: product.id)
    end

    assert_response :success
  end

およびcurrent_cart

 def current_cart 
      Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound 
      cart = Cart.create 
      session[:cart_id] = cart.id
      cart
    end

2回目の更新

あなたが提案したように、私は工場を更新しました。

私のテストは次のようになります。

 test "should get create" do
  user = FactoryGirl.create(:user)
  cart = FactoryGirl.create(:cart_with_1_row)
  product = FactoryGirl.create(:product)
  sign_in(user)

  product = FactoryGirl.create(:product)
  assert_difference('ProductRequest.count') do
    post :create, { product_request: FactoryGirl.attributes_for(:product_request, user_id: user.id, product_id: product.id, cart_id: cart.id) }
  end

  assert_response :success
end

これがテストコンソールにあります。

irb(main):016:0> a = { product_request: FactoryGirl.attributes_for(:product_request, user_id: user.id, product_id: product.id, cart_id: cart.id) }
=> {:product_request=>{:quantity=>10, :street=>"123 street", :city=>"Some City", :state=>"Iowa", :zip=>"13829", :user_id=>1, :product_id=>2, :cart_id=>1}}
4

2 に答える 2

1

まず第一にCartRow.find_all_by_cart_id(cart.id)、これは良いデザインではありません。カートモデルにその行を尋ねると、はるかに優れています。たとえば、次のようになります。rows = cart.rows

問題は、カートの中に列がないことだと思います。

私が見るように、あなたはセッションにカートIDを保存しますが、テストでコントローラーを呼び出すとき、あなたはセッションを提供しません。コントローラを呼び出す前に、カートとカートの行を作成し、cart_idをセッションに保存する必要があります。また、現在のセッションとセッションをcart_idとマージすることが重要です。例えば:

test "should get create" do
  user = FactoryGirl.create(:user)
  cart = FactoryGirl.create(:cart_with_1_row)
  sign_in(user)

  product = FactoryGirl.create(:product)
  assert_difference('ProductRequest.count') do
    post :create, { product_request: FactoryGirl.attributes_for(:product_request, user: user.id, product: product.id) }, { cart_id: cart.id }.merge(session)
  end

  assert_response :success
end

また、カートとカートの行ファクトリを更新する必要があります。

FactoryGirl.define do
  factory :cart do
    factory :cart_with_1_row do
      after(:create) do |cart|
        FactoryGirl.create(:cart_row, cart: cart)
      end
    end
  end

  factory :cart_row do
    cart
  end
end

CartRowモデルは次のようになります。

class CartRow < ActiveRecord::Base
  belongs_to :cart
end
于 2012-09-01T05:54:40.227 に答える
0

問題は間違いなくカートから来ているようです。

FactoryGirlでカートを作成することに対処したくない場合(私がお勧めします)、テストでcurrent_cardをスタブアウトするだけで、同じことができます。

ただし、モックはFactoryGirlでカートを作成するよりもはるかに複雑であり、これを将来使用することを計画している場合は、FactoryGirlが間違いなく最適な方法です。

于 2012-08-22T22:32:35.503 に答える