使用 Test::Unit 测试模块

发布于 2024-08-11 21:49:28 字数 634 浏览 11 评论 0原文

我在尝试使用 Test::Unit 测试模块时遇到问题。我以前做的是这样的:

my_module.rb:

class MyModule
  def my_func
    5 # return some value
  end
end

test_my_module.rb:

require 'test/unit'
require 'my_module'

class TestMyModule < Unit::Test::TestCase
  include MyModule

  def test_my_func
    assert_equal(5, my_func) # test the output value given the input params
  end
end

现在的问题是,如果 my_module 声明了一个初始化方法,它就会包含在测试类中,并且这会导致一堆问题,因为 Test::Unit 似乎覆盖/生成了一个初始化方法。所以我想知道测试模块的最佳方法是什么?

我还想知道我的模块此时是否应该成为一个类,因为初始化方法是为了初始化某些东西的状态而创建的。意见?

提前致谢 !

I encountered a problem when trying to test a module with Test::Unit. What I used to do is this:

my_module.rb:

class MyModule
  def my_func
    5 # return some value
  end
end

test_my_module.rb:

require 'test/unit'
require 'my_module'

class TestMyModule < Unit::Test::TestCase
  include MyModule

  def test_my_func
    assert_equal(5, my_func) # test the output value given the input params
  end
end

Now the problem is, if my_module declares an initialize method, it gets included in the test class and this causes a bunch of problems since Test::Unit seems to override/generate an initialize method. So I'm wondering what is the best way to test a module?

I'm also wondering wether my module should become a class at this point since the initialize method is made for initializing the state of something. Opinions?

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼前雾蒙蒙 2024-08-18 21:49:28

在模块中包含 initialize 方法对我来说感觉非常错误,所以我至少会重新考虑这一点。

不过,为了更直接地回答有关将其作为模块进行测试的问题,我将创建一个新的空类,将您的模块包含在其中,创建该类的实例,然后针对该实例进行测试:

class TestClass
  include MyModule
end

class TestMyModule < Unit::Test::TestCase
  def setup
    @instance = TestClass.new
  end

  def test_my_func
    assert_equal(5, @instance.my_func) # test the output value given the input params
  end
end

Including an initialize method in a module feels very wrong to me, so I'd rethink that at the very least.

To answer your question about testing this as a module more directly, though, I would create a new, empty class, include your module in it, create an instance of that class, and then test against that instance:

class TestClass
  include MyModule
end

class TestMyModule < Unit::Test::TestCase
  def setup
    @instance = TestClass.new
  end

  def test_my_func
    assert_equal(5, @instance.my_func) # test the output value given the input params
  end
end
梦开始←不甜 2024-08-18 21:49:28

是的,你的初始化肯定会表明你要去上课。 ruby 中的模块通常感觉就像其他语言中的接口,只要在包含该模块时实现一些基本的东西,您将免费获得很多东西。

Enumerable 是一个很好的例子,只要你定义 [] 并且每次包含 Enumerable 时你都会突然得到 pop、push 等。

所以我对测试模块的直觉是你可能应该测试包含该模块的类而不是测试模块本身,除非该模块被设计为不包含在任何内容中,否则它只是一种代码存储机制。

Yeah, your initialize should definitely suggest that you're going towards a class. A module in ruby often feels like an interface in other languages, as long as you implement some basic things when you include the module you'll get a lot for free.

Enumerable is a great example, as long as you define [] and each when you include Enumerable you suddenly get pop, push, etc.

So my gut feeling about testing modules is that you should probably be testing classes that include the module rather than testing the module itself unless the module is designed to not be included in anything, it's simply a code storage mechanism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文