在Corda中的合同测试中,是否可以在分类帐内以编程方式创建多个交易?

时间:2019-11-14 18:20:32

标签: kotlin corda

在Corda的合同测试中,是否可以在分类帐内以编程方式创建多个交易?当我删除forEach循环并为每个事务复制并粘贴代码(更改输出状态以匹配我要测试的内容)时,一切都会按预期进行。但是,如果像代码示例中那样尝试重构它并在forEach循环内以编程方式生成事务,则测试将失败。

@Test
fun simpleTest() {
    ledger {
        listOf(...).forEach { 
            transaction {
                command(participants, Commands.Command())
                input(Contract.ID, inputState)
                output(Contract.ID, outputState)
                failsWith("message")
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

回应您对我的评论的回复;我认为您应该尝试使用tweak
在下面的示例中,您将看到我的交易如何具有相同的命令和输出,但是我可以对其进行调整并尝试不同的输入:

@Test
@DisplayName("Testing different inputs.")
public void testDifferentInputs() {
    CustomState goodInput = new CustomState();
    CustomState badInput1 = new CustomState();
    CustomState badInput2 = new CustomState();
    CustomState output = new CustomState();

    ledger(ledgerServices, l -> {
        l.transaction(tx -> {
            // Same output and command.
            tx.output("mypackage.CustomState", output);
            tx.command(Collections.singletonList(myNode.getPublicKey()), new CustomStateContract.Commands.Create());
            // Tweak allows to tweak the transaction, in this case we're using bad input #1 and the transaction should fail.
            tx.tweak(tw -> {
                tw.input("mypackage.CustomState", badInput1);               
                return tw.failsWith("Bad Input State.");
            });
            // Tweak allows to tweak the transaction, in this case we're using bad input #2 and the transaction should fail.
            tx.tweak(tw -> {
                tw.input("mypackage.CustomState", badInput2);               
                return tw.failsWith("Bad Input State.");
            });
            // After the tweak we are using the good input and the transaction should pass.
            tx.input("mypackage.CustomState", goodInput);           
            return tx.verifies();
        });
        return Unit.INSTANCE;
    });
}

此处有更多示例:https://docs.corda.net/tutorial-test-dsl.html