I have a test that creates the following error:
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>.
How do I trouble shoot this? Specifically, how can I get a more specific detailed error?
Here is my test:
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
and here is my controller:
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
I believe the issue is that I don’t have a cart defined. How do I create a cart that unit::test can see? I’ve tried using FactoryGirl to create a cart, but that didn’t seem to work.
carts_factory.rb
FactoryGirl.define do
factory :cart do
end
end
Updated test:
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
and current_cart
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
Second Update
I’ve updated the factories as you’ve suggested.
Here is what my test now looks like:
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
Here’s it in a test console:
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}}
First of all,
CartRow.find_all_by_cart_id(cart.id), this is not good design. Much better when you ask Cart model for its row, for example:rows = cart.rowsI think the issue is that you don’t have rows inside you cart.
As I see you store cart id in session but when you call controller in test you does not provide session. You need create cart and cart’s rows and when store cart_id in session before you call controller. And it is important to merge current session and session with cart_id. For example:
Also you need update your cart and cart’s row factories:
I think your CartRow model looks like: