我正在为我的CorDapp写一份合同测试,但是我已经陷入困境,因为它没有表现出应有的效果。
这是合同代码(仅提供相关部分)
when (command.value ) {
is LoanCommands.Issue -> requireThat {
"No Inputs should be consumed when issuing a Loan" using ( tx.inputs.isEmpty())
"Only a single output must be created when issugin a Loan" using (tx.outputs.size == 1)
"Outputs must not be empty" using (tx.outputs.isNotEmpty())
然后这是我正在运行的测试
@Test
fun issueTransactionMustHaveOneOutputOfTypeLoanContract() {
ledger {
transaction {
command(loanState.issuer.owningKey) { LoanContract.LoanCommands.Issue() }
timeWindow(ZonedDateTime.of(LocalDate.now(), time, zoneId).toInstant())
this `fails with` "Outputs must not be empty"
}
transaction {
command(loanState.issuer.owningKey) { LoanContract.LoanCommands.Issue() }
timeWindow(ZonedDateTime.of(LocalDate.now(), time, zoneId).toInstant())
output { loanState } // Two outputs fails.
output { dummyState}
this `fails with` "Only a single output must be created when issugin a Loan"
}
transaction {
command(loanState.issuer.owningKey) { LoanContract.LoanCommands.Issue() }
output { loanState } // One output passes.
timeWindow(ZonedDateTime.of(LocalDate.now(), time, zoneId).toInstant())
this.verifies()
}
}
}
请注意,第二个事务正确失败(当我删除第一个事务时),但第一个事务给我这个错误
2017-08-14 12:32:15,250 main WARN Unable to instantiate org.fusesource.jansi.WindowsAnsiOutputStream
java.lang.AssertionError: Expected exception but didn't get one
at net.corda.testing.Verifies$DefaultImpls.failsWith(LedgerDSLInterpreter.kt:62)
at net.corda.testing.TransactionDSLInterpreter$DefaultImpls.failsWith(TransactionDSLInterpreter.kt)
at net.corda.testing.TestTransactionDSLInterpreter.failsWith(TestDSL.kt:89)
at net.corda.testing.Verifies$DefaultImpls.fails with(LedgerDSLInterpreter.kt:76)
at net.corda.testing.TransactionDSLInterpreter$DefaultImpls.fails with(TransactionDSLInterpreter.kt)
at net.corda.testing.TestTransactionDSLInterpreter.fails with(TestDSL.kt:89)
at net.corda.testing.TransactionDSL.fails with(TransactionDSLInterpreter.kt)
at com.template.contractTests.LoanContractIssueTest$issueTransactionMustHaveOneOutputOfTypeLoanContract$1$1.invoke(LoanContractIssueTest.kt:123)
at com.template.contractTests.
为什么它不会引发异常?看起来tx.outputs.isNotEmpty()
无效。
感谢任何帮助
答案 0 :(得分:1)
好问题 - 从您的代码中我没有看到问题。但请注意,您的规则似乎重叠。您测试只有一个输出,然后测量有> 0输出。第二个测试可以被删除,它永远不会捕获第一个没有的东西。
答案 1 :(得分:1)
问题是第一次测试中的事务不包含任何输入或输出。要使交易有效,必须由所有投入和产出的合同接受。由于您没有输入或输出,因此交易始终有效。
当您有两个输出时,这不是问题,因为在这种情况下由于存在两个输出而运行verify
函数。