我正在尝试使用 spock 为我拥有的方法编写测试。
该方法看起来像这样 -
InvoiceView getInvoiceDetailView(String invoiceNumber, Boolean isNew) {
InvoiceView detailView;
if (isNew) {
newInvoiceDao.em = billingEm;
//call InvoiceDao
List<GroovyRowResult> invoiceSummaryRowResult = billingAdmin.rows(InvoiceQueries.GET_NEW_INVOICE_SUMMARIES_BY_NUMBER, [invoiceNumber:invoiceNumber]);
List<GroovyRowResult> invoiceDetailsRowResult = billingAdmin.rows(InvoiceQueries.GET_NEW_INVOICE_DETAILS_BY_NUMBER, [invoiceNumber:invoiceNumber]);
InvoiceModel invoiceModel = newInvoiceDao.getInvoice(invoiceNumber);
detailView = new InvoiceView(invoice:invoiceModel,
summary:invoiceSummaryRowResult,
details:invoiceDetailsRowResult);
} ...
我的测试只是试图断言,当调用此方法时将 true 作为 isNew 参数的值传入,将返回一个有效的 InvoiceView 对象。
在我的 spock 测试中,我创建了一些我希望从该方法返回的对象:
def expectedInvoiceModel = new InvoiceModel()
def expectedInvoiceSummaryRowResult = new ArrayList<GroovyRowResult>()
def expectedInvoiceDetailsRowResult = new ArrayList<GroovyRowResult>()
def expectedInvoiceView = new InvoiceView(invoice:expectedInvoiceModel,
summary:expectedInvoiceSummaryRowResult,
details:expectedInvoiceDetailsRowResult)
我还模拟了我将调用的 dao 对象:
def setup() {
...
service.billingAdmin = Mock(Sql)
...
}
所以最后到了Spock夹具上。我已将有问题的比较行注释掉并进行了解决。我的问题是 - 为什么我的“then”块中的第一行失败?
def "getInvoiceDetailView returns valid InvoiceView for new invoice"() {
given:
service.billingAdmin.rows(InvoiceQueries.GET_NEW_INVOICE_SUMMARIES_BY_NUMBER, [invoiceNumber:invoiceNumber]) >> expectedInvoiceSummaryRowResult
service.billingAdmin.rows(InvoiceQueries.GET_NEW_INVOICE_DETAILS_BY_NUMBER, [invoiceNumber:invoiceNumber]) >> expectedInvoiceDetailsRowResult
service.newInvoiceDao.getInvoice(_) >> expectedInvoiceModel
when:
def result = service.getInvoiceDetailView(invoiceNumber, true)
then:
//result == expectedInvoiceView -- Why doesn't this work?
result.invoice == expectedInvoiceModel
result.summary == expectedInvoiceSummaryRowResult
result.details == expectedInvoiceDetailsRowResult
}