Minitest
Minitest/AssertEmpty
Minitest/AssertEqual
Minitest/AssertInDelta
Minitest/AssertIncludes
Minitest/AssertInstanceOf
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.4 |
- |
强制测试使用assert_instance_of(Class, object)而不是assert(object.instance_of?(Class))。
Minitest/AssertKindOf
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
始终 |
0.10 |
0.34 |
强制测试使用assert_kind_of(Class, object)而不是assert(object.kind_of?(Class))。
Minitest/AssertMatch
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.6 |
- |
强制测试使用assert_match而不是使用assert(matcher.match(string))。
Minitest/AssertNil
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.1 |
- |
强制测试使用assert_nil而不是使用assert_equal(nil, something),assert(something.nil?)或assert_predicate(something, :nil?)。
Minitest/AssertOperator
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
始终 |
0.32 |
- |
强制使用 assert_operator(expected, :<, actual) 代替 assert(expected < actual)。
Minitest/AssertOutput
Minitest/AssertPathExists
Minitest/AssertPredicate
Minitest/AssertRaisesWithRegexpArgument
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
否 |
0.22 |
0.26 |
检查 assert_raises 是否使用正则表达式字面量作为参数。参数应为异常类。可选地,最后一个参数可以是自定义消息字符串,以帮助解释失败。无论哪种方式,它都不是 exception.message 进行比较的参数。抛出的异常将被返回,可用于与正则表达式匹配。
Minitest/AssertRespondTo
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.3 |
- |
强制使用 assert_respond_to(object, :do_something) 代替 assert(object.respond_to?(:do_something))。
Minitest/AssertSame
Minitest/AssertSilent
Minitest/AssertTruthy
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
否 |
始终(不安全) |
0.2 |
0.27 |
强制测试使用assert(actual)而不是使用assert_equal(true, actual)。
安全
此 cop 不安全,因为可能需要 true 而不是 truthy。当这是一个变量或方法返回值时,无法防止误报。
assert_equal(true, 'truthy') # failure
assert('truthy') # success
Minitest/AssertWithExpectedArgument
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
否 |
否 |
0.11 |
0.26 |
尝试检测用户何时意外使用assert,而他们本应使用assert_equal。
assert 方法的第二个参数名为message 和msg 是允许的。因为它们的名称被推断为消息参数。
|
Minitest/DuplicateTestRun
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
否 |
0.19 |
- |
如果 Minitest 类从另一个类继承,它也会继承其方法,导致 Minitest 运行父类的测试方法两次。
此 cop 检测到是否有两个测试类,一个从另一个继承,并且两者都有测试方法。在这种情况下,此 cop 将在子类中添加一个违规。
示例
# bad
class ParentTest < Minitest::Test
def test_parent # it will run this test twice.
end
end
class ChildTest < ParentTest
def test_child
end
end
# good
class ParentTest < Minitest::Test
def test_parent
end
end
class ChildTest < Minitest::Test
def test_child
end
end
# good
class ParentTest < Minitest::Test
end
class ChildTest
def test_child
end
def test_parent
end
end
Minitest/GlobalExpectations
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.7 |
0.26 |
检查已弃用的全局期望并将其自动更正为使用期望格式。
示例
EnforcedStyle: any(默认)
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError
# good
_(musts).must_equal expected_musts
_(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError
expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError
value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError
EnforcedStyle: _
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError
expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError
value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError
# good
_(musts).must_equal expected_musts
_(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError
EnforcedStyle: expect
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError
_(musts).must_equal expected_musts
_(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError
value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError
# good
expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError
EnforcedStyle: value
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError
_(musts).must_equal expected_musts
_(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError
expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError
# good
value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError
Minitest/LifecycleHooksOrder
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
始终 |
0.28 |
- |
检查生命周期钩子是否按执行顺序声明。
示例
# bad
class FooTest < Minitest::Test
def teardown; end
def setup; end
end
# good
class FooTest < Minitest::Test
def setup; end
def teardown; end
end
# bad (after test cases)
class FooTest < Minitest::Test
def test_something
assert foo
end
def setup; end
def teardown; end
end
# good
class FooTest < Minitest::Test
def setup; end
def teardown; end
def test_something
assert foo
end
end
# good (after non test case methods)
class FooTest < Minitest::Test
def do_something; end
def setup; end
def teardown; end
end
Minitest/LiteralAsActualArgument
Minitest/MultipleAssertions
Minitest/NonExecutableTestMethod
Minitest/NonPublicTestMethod
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
否 |
0.27 |
- |
检测非 public(标记为 private 或 protected)测试方法。Minitest 仅运行 public 的测试方法。
示例
# bad
class FooTest
private # or protected
def test_does_something
assert_equal 42, do_something
end
end
# good
class FooTest
def test_does_something
assert_equal 42, do_something
end
end
# good (not a test case name)
class FooTest
private # or protected
def does_something
assert_equal 42, do_something
end
end
# good (no assertions)
class FooTest
private # or protected
def test_does_something
do_something
end
end
Minitest/RefuteEmpty
Minitest/RefuteEqual
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.3 |
- |
强制使用 refute_equal(expected, object) 而不是使用 assert(expected != actual) 或 assert(! expected == actual)。
Minitest/RefuteFalse
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
否 |
始终(不安全) |
0.3 |
0.27 |
强制使用 refute(object) 而不是使用 assert_equal(false, object)。
安全
此规则不安全,因为它无法检测到第二个参数为 nil 时的失败。当这是一个变量或方法返回值时,无法防止误报。
assert_equal(false, nil) # failure
refute(nil) # success
Minitest/RefuteInDelta
Minitest/RefuteIncludes
Minitest/RefuteInstanceOf
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.4 |
- |
强制使用 refute_instance_of(Class, object) 而不是使用 refute(object.instance_of?(Class))。
Minitest/RefuteKindOf
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
始终 |
0.10 |
0.34 |
强制使用 refute_kind_of(Class, object) 而不是使用 refute(object.kind_of?(Class))。
Minitest/RefuteMatch
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.6 |
- |
强制测试使用 refute_match 而不是使用 refute(matcher.match(string))。
Minitest/RefuteNil
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.2 |
- |
强制测试使用 refute_nil 而不是使用 refute_equal(nil, something)、refute(something.nil?) 或 refute_predicate(something, :nil?)。
Minitest/RefuteOperator
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
始终 |
0.32 |
- |
强制使用 refute_operator(expected, :<, actual) 而不是 refute(expected < actual)。
Minitest/RefutePathExists
Minitest/RefutePredicate
Minitest/RefuteRespondTo
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
启用 |
是 |
始终 |
0.4 |
- |
强制测试使用 refute_respond_to(object, :do_something) 而不是 refute(object.respond_to?(:do_something))。
Minitest/RefuteSame
Minitest/ReturnInTestMethod
Minitest/SkipEnsure
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
否 |
0.20 |
0.26 |
检查即使 skip 也调用 ensure。在跳过测试时调用 ensure 是不可预期的。如果使用条件 skip,则检查 ensure 也按条件调用。
另一方面,它接受在 rescue 中使用的 skip,因为 ensure 可能是 begin 设置过程的拆卸过程。
示例
# bad
def test_skip
skip 'This test is skipped.'
assert 'foo'.present?
ensure
do_something
end
# bad
def test_conditional_skip
skip 'This test is skipped.' if condition
assert do_something
ensure
do_teardown
end
# good
def test_skip
skip 'This test is skipped.'
begin
assert 'foo'.present?
ensure
do_something
end
end
# good
def test_conditional_skip
skip 'This test is skipped.' if condition
assert do_something
ensure
if condition
do_teardown
end
end
# good
def test_skip_is_used_in_rescue
do_setup
assert do_something
rescue
skip 'This test is skipped.'
ensure
do_teardown
end
Minitest/TestFileName
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
否 |
0.26 |
- |
检查测试文件名是否以test_开头或以_test.rb结尾。检查定义以Test结尾的类的文件。不遵循此约定可能会导致测试无法运行。
Minitest/TestMethodName
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
始终 |
0.10 |
- |
强制测试方法名以test_前缀开头。它旨在防止忘记以test_开头测试方法名的测试无法执行。
Minitest/UnspecifiedException
| 默认启用 | 安全 | 支持自动更正 | 添加版本 | 更改版本 |
|---|---|---|---|---|
待定 |
是 |
否 |
0.10 |
- |
检查assert_raises中是否指定了错误。