Forum: Ruby on Rails new to rails; problem with testing section in Agile book

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A06242f9d05b5925ef65e3fa5f52ad4b?d=identicon&s=25 Nate (Guest)
on 2006-05-19 04:08
Hello, I should give the expected disclaimer that I am brand new to
rails and am going through the Agile web development with Rails book
right now. I am in the testing section and I can't get past an error.

this is my cart_test.rb test file:

require File.dirname(__FILE__) + '/../test_helper'

class CartTest < Test::Unit::TestCase
  fixtures :products
	def setup
		@cart = Cart.new
	end
	# Replace this with your real tests.
	def test_add_unique_products
		@cart.add_product products(:version_control_book)
		@cart.add_product products(:automation_book)
		assert_equal products(:version_control_book).price +
products(:automation_book).price, @cart.total_price
		assert_equal 2, @cart.items.size
	end
	def test_add_duplicate_product
		@card.add_product products(:version_control_book)
		@card.add_product products(:version_control_book)
		assert_equal 2*products(:version_control_book).price,
@cart.total_price
		assert_equal 1, @cart.items.size
	end
end


and I am getting this error:

1) Error:
test_add_duplicate_product(CartTest):
NoMethodError: You have a nil object when you didn't expect it!
The error occured while evaluating nil.add_product
    test/unit/cart_test.rb:17:in `test_add_duplicate_product'





If there is more information i need to post please let me know.
Any help is appreciated!

Thanks,

Nate
42a64442dad4115d8104d5c09289767d?d=identicon&s=25 Simon Robson (Guest)
on 2006-05-19 04:24
(Received via mailing list)
Hi Nate,

Nate wrote:
> 	def test_add_duplicate_product
> 		@card.add_product products(:version_control_book)
> 		@card.add_product products(:version_control_book)

Should that be @cart in the two lines above? Looks like a simple typo -
but they can be the most frustrating!

> 1) Error:
> test_add_duplicate_product(CartTest):
> NoMethodError: You have a nil object when you didn't expect it!
> The error occured while evaluating nil.add_product
>     test/unit/cart_test.rb:17:in `test_add_duplicate_product'
>

Simon
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-05-19 04:31
(Received via mailing list)
On May 18, 2006, at 10:08 PM, Nate wrote:
> 	def test_add_duplicate_product
> 		@card.add_product products(:version_control_book)
> 		@card.add_product products(:version_control_book)
> 		assert_equal 2*products(:version_control_book).price,
> @cart.total_price
> 		assert_equal 1, @cart.items.size
> 	end

I'm guessing you missed the two times you typed "@card" instead of
"@cart"...

-Brian
A06242f9d05b5925ef65e3fa5f52ad4b?d=identicon&s=25 Nate (Guest)
on 2006-05-19 08:50
Brian and Simon, I must say that I am COMPLETELY embarassed!  I had been
looking at that for multiple hours and couldn't find it at all.  I
thought it had something to do with the book written pre rails 1.0 so i
did some research about the changes made in the newer version and
learned about use_transitional_fixtures vs use_instantiated_fixtures.
Anyway, that last part is irrelavant; thanks so much for find my glaring
mistake, i wish i could erase this post! LOL

thanks again,

Nate
This topic is locked and can not be replied to.