views:

32

answers:

1

Hello I have a trouble with Ruby unit testing, I'm new to it so some help would be lovely

class TestItem < Test::Unit::TestCase def setUp @item=Item.new('Food','Burger',120) end def testGetType assert_equal(@item.getType,'Food') end end

Here the value of instance variable @item takes nil when I declare it in setUp() and use it in test functions! So I get an error like no method 'getType' for nil-class

But when I directly use it like assert_equal(Item.new('Food','Burger',120).getType,'Food'),it works fine.

Please point out to my mistakes, thanks in advance

+1  A: 

The name of the setup method is setup, not setUp. In fact, you will never find a method called setUp in Ruby, since the standard Ruby style for method naming is snake_case, not camelCase. (The same applies to getType and testGetType, BTW. It should be get_type and test_get_type. Well, actually, in Ruby, getters aren't prefixed with get, so really it should be type and test_type. But note that in Ruby, all objects already have type method, although that is deprecated.)

Jörg W Mittag
Thanks a lot, I'm from python background so.. Thanks mate :)
ShyamLovesToCode