Nicholas Rogers
Nicholas Rogers

Reputation: 360

In Corda unit tests is there a way to inspect the outcome of a flow before committing it to the ledger?

I have a flow that queries an oracle to get signed random data (representing a dice roll) which then becomes the output of a resulting transaction. Specifically, a new DiceRollState is issued.

In my unit tests, I need to ensure a given DiceRollState has a certain value (reflecting the number of pips on the dice) to test resulting logic. For example - I need to do task X when the DiceRollState has a value of 5 and another action when it has a value of 6.

Below is a snippet of code I'm using to run the flow in my unit test.

    val rollDiceFlow = RollDiceFlow(gameBoardState.linearId)
    val futureWithDiceRollPlayer1 = nodeA.startFlow(rollDiceFlow)

    // Can I do anything here to check the value 
    // of the outputted DiceRollState and revert / Rerun as required?

    network.runNetwork() 
    futureWithDiceRollPlayer1.getOrThrow()

Is there any way to revert if the outcome of a mock flow is undesirable? Could I do any of the following?

Upvotes: 0

Views: 82

Answers (1)

Nicholas Rogers
Nicholas Rogers

Reputation: 360

The answer here (unfortunately) is that this isn't a testable design pattern for Corda. Reverting a mock network to a previous state might be a useful feature for current testing but isn't currently available.

The solutions are to:

  • Refactor the flow to accept optional parameters for the dice roll. This enables a deterministic dice roll to be constructed and passed in.
  • Create a mock oracle that returns deterministic data, which again would enable a deterministic dice roll

I opted to refactor rollDiceFlow to accept a constructed dice roll. The constructed dice roll is retrieved by the oracle in a wrapping flow in the actual CorDapp, but can be provided directly for testing purposes.

Upvotes: 1

Related Questions