我想使用spock来查找类中的方法是否被调用。但是当我尝试验证它时,when block 表示该方法从未被调用。
public class Bill {
public Bill(Integer amount) {
this.amount = amount;
}
public void pay(PaymentMethod method) {
Integer finalAmount = amount + calculateTaxes();
method.debit(finalAmount);
}
private Integer calculateTaxes() {
return 0;
}
private final Integer amount;
}
public class PaymentMethod {
public void debit(Integer amount) {
// TODO
}
public void credit(Integer amount) {
// TODO
}
}
import spock.lang.Specification
import spock.lang.Subject
class BillSpec extends Specification {
@Subject
def bill = new Bill(100)
def "Test if charge calculated"() {
given:
PaymentMethod method = Mock()
when:
bill.pay(method)
then:
1 * method.debit(100)
// 1 * bill.calculateTaxes() // Fails with (0 invocations) error
0 * _
}
}
在上面的示例中,我想做的就是验证是否正在调用calculateTaxes,但测试失败(0 次调用)。我尝试使用 spy ,但不确定语法是什么,因为 Bill 采用参数化构造函数。
最佳答案
当您监视 Bill
实例时,您可以测试 calculateTaxes()
调用,如下所示:
class SpyTestSpec extends Specification {
def "Test if charge calculated"() {
given:
def bill = Spy(new Bill(100))
PaymentMethod method = Mock()
when:
bill.pay(method)
then:
1 * method.debit(100)
1 * bill.calculateTaxes()
1 * bill.pay(method)
0 * _
}
}
另一件重要的事情是使 calculateTaxes()
方法对测试可见,否则它仍然会失败:
public Integer calculateTaxes() { ... }
请注意,如果您想测试是否没有调用任何其他内容,那么您还应该添加:
1 * bill.pay(method)
关于groovy - 使用 Spock 验证非 spy 方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55276738/