Ruby单元测试-安装程序中声明的实例变量的值为nil
您好,我在Ruby单元测试方面遇到了问题,我是新手,所以需要一些帮助Ruby单元测试-安装程序中声明的实例变量的值为nil,ruby,unit-testing,testunit,Ruby,Unit Testing,Testunit,您好,我在Ruby单元测试方面遇到了问题,我是新手,所以需要一些帮助 class TestItem < Test::Unit::TestCase def setUp **@item**=Item.new('Food','Burger',120) end def testGetType assert_equal(**@item**.getType,'Food') end end class TestItem方法,尽管它已被弃用。)非常感谢,我来自python背景,所以。。谢
class TestItem < Test::Unit::TestCase
def setUp
**@item**=Item.new('Food','Burger',120)
end
def testGetType
assert_equal(**@item**.getType,'Food')
end
end
class TestItem
这里,当我在setUp()中声明实例变量@item并在测试函数中使用它时,它的值为nil!所以我得到了一个错误,比如对于nil类没有方法'getType'
但是当我直接使用它,比如assert_equal(Item.new('Food','Burger',120)。getType,'Food'),它工作得很好
请指出我的错误,提前谢谢设置方法的名称是
设置
,而不是设置
。事实上,在Ruby中永远找不到名为setUp
的方法,因为方法命名的标准Ruby样式是snake\u case
,而不是camelCase
。(同样适用于getType
和testGetType
,顺便说一句。它应该是get\u type
和test\u get\u type
。实际上,在Ruby中,getter的前缀不是get
,所以它实际上应该是type
和test\u type
。但请注意,在Ruby中,所有对象都已经有type
)>方法,尽管它已被弃用。)非常感谢,我来自python背景,所以。。谢谢,伙计:)