如何在 ruby 模块中包含单元测试?
我试图将模块的单元测试包含在与模块本身相同的源文件中,遵循 Perl modulino 模型。
#! /usr/bin/env ruby
require 'test/unit'
module Modulino
def modulino_function
return 0
end
end
class ModulinoTest < Test::Unit::TestCase
include Modulino
def test_modulino_function
assert_equal(0, modulino_function)
end
end
现在,我可以运行执行此源文件的单元测试。
但是,当我从另一个脚本请求/加载它们时,它们也会运行。如何避免这种情况?
是否有更惯用的方法可以使用 Ruby 实现此目的,除非不鼓励这种做法?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我个人而言,我从未听说过有人尝试用 Ruby 来做到这一点。这绝对不是标准做法。也就是说,您也许可以利用这个技巧:
只有直接执行文件时,
if
块中的代码才会执行,而其他库或应用程序需要时则不会执行。更多 ruby 技巧在这里: http://www.rubyinside.com/21-ruby-技巧-902.html
Personally I've never heard of anyone trying to do this in Ruby. It's definitely not a standard practice. That said you may be able to leverage this trick:
The code in the
if
block will only execute if the file is executed directly, not if it's required by another library or application.More ruby tricks here: http://www.rubyinside.com/21-ruby-tricks-902.html
实际上,这在 Ruby 中并不少见,尽管这在 Rails 中肯定不是常见的做法。
您可能遇到的问题之一与 这篇文章表明模块确实应该包含在类中以便测试它们。当然可以通过将模块包含在测试用例中来测试模块,但是您正在测试该模块在混合到 Test::Unit::TestCase 中时是否可以工作,而不是在将其混合到更有用的东西中时它会工作。
因此,单元测试可能应该存在于类文件中,或者如果您只想使用通用方法,请使用类函数而不是模块。
It's actually not that uncommon in Ruby though it's certainly not the common practice in Rails.
One of the issues you may be running into is the same as this post which is that modules really should be included in classes in order to test them. It's certainly possible to test a module by including it in your test case but you're testing whether the module works when mixed into Test::Unit::TestCase, not that it'll work when you mix it into something more useful.
So unit tests should probably live on the class file or if you just want generally available methods use a class function instead of a module.
您可以使用 minitest 在模块源代码本身中包含单元测试。
试试这个例子:
这里我创建了一个名为 Foo 的类,它继承自 String 类。
然后我创建了两个单元测试。在第一个测试中,我检查是否可以实例化
Foo 类的对象。在第二个测试中,我检查 Foo 类的实例化对象是否是一种 String。
如果此代码编写在名为 foo.rb 的文件中,我可以简单地使用以下命令运行测试:
Minitest 执行速度很快。 “pride”模块允许您以彩色字体输出测试结果,非常养眼。
You can include unit tests inside the module source code itself using minitest.
Try this example:
Here I created a class called Foo, which inherits from the class String.
Then I created two unit tests. In the first test, I check that I can instantiate
an object of class Foo. In the second test, I check that the instantiated object of class Foo is a kind of String.
If this code is written in a file called foo.rb, I can simply run the tests using this command:
Minitest is fast to execute. The "pride" module allows you to output the test result in color fonts, which is nice on the eye.
刚刚找到一种方法来防止在脚本需要模块时执行单元测试。
.../lib/ruby/1.8/test/
中的unit.rb
中有一个标志设置为 true。结合 samg 技巧(再次感谢),我们可以写:
Just found one way to prevent the unit test from being executed when the module is required from a script. There is a flag in
unit.rb
in.../lib/ruby/1.8/test/
to set to true.Combined with samg trick (thanks again), we can write: