Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby单元测试-安装程序中声明的实例变量的值为nil_Ruby_Unit Testing_Testunit - Fatal编程技术网

Ruby单元测试-安装程序中声明的实例变量的值为nil

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背景,所以。。谢

您好,我在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
这里,当我在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背景,所以。。谢谢,伙计:)