0

您好,我在 Ruby 单元测试方面遇到了问题,我是新手,所以一些帮助会很可爱

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

当我在 setUp() 中声明实例变量 @item 并在测试函数中使用它时,这里实例变量 @item 的值为零!所以我得到一个错误,比如没有方法'getType' for nil-class

但是当我像 assert_equal(Item.new('Food','Burger',120).getType,'Food') 一样直接使用它时,它工作正常。

请指出我的错误,提前谢谢

4

1 回答 1

2

setup方法的名称是setup,不是setUp。事实上,你永远找不到在 Ruby 中调用的方法setUp,因为标准的 Ruby 方法命名风格是snake_case, not camelCase。(这同样适用于getTypeand testGetType,顺便说一句。它应该是get_typeand test_get_type。实际上,在 Ruby 中,getter 不以 为前缀get,所以实际上它应该是typeand test_type。但请注意,在 Ruby 中,所有对象都已经有type方法,尽管那是已弃用。)

于 2010-10-08T23:05:24.667 回答