cod*_*eme 5 testing groovy unit-testing mocking
以下是我正在寻找的行为.我想要一个Groovy MockFor ignore方法来调用一个demand方法,而不是dontIgnoreMe()
直接调用该方法的ignore方法.
从本质上讲,我想dontIgnoreMe()
用mock 替换我,并ignoreMe()
调用mock(我用metaClass做过)看起来类别可能是更好的解决方案.我将在下周调查一下,看看:groovy nabble feed
import groovy.mock.interceptor.MockFor
class Ignorable {
def dontIgnoreMe() { 'baz' }
def ignoreMe() { dontIgnoreMe() }
}
def mock = new MockFor(Ignorable)
mock.ignore('ignoreMe')
mock.demand.dontIgnoreMe { 'hey' }
mock.use {
def p = new Ignorable()
assert p.dontIgnoreMe() == 'hey'
assert p.ignoreMe() == 'hey'
}
Run Code Online (Sandbox Code Playgroud)
Assertion failed:
assert p.ignoreMe() == 'hey'
| | |
| baz false
Ignorable@6879c0f4
Run Code Online (Sandbox Code Playgroud)
对于常规开发人员,我强烈推荐 Spock Framework!
使用Spy,如下面的代码所示:
def "Testing spy on real object with spock"() {
given:
Ignorable ignorable = Spy(Ignorable)
when:
ignorable.dontIgnoreMe() >> { 'hey' }
then:
ignorable.ignoreMe() == 'hey'
ignorable.dontIgnoreMe() == 'hey'
}
Run Code Online (Sandbox Code Playgroud)