# How to test a command?
Open the respective command test suite, ex: CreateCommandTestSuite.scala
Grab the CommandFixture, provide the Input (if any) & verify the post & pre conditions.
For example:
- To verify if all the post conditions pass given the right input:
test("create first lottery", Tag("create"), ptest) { grab[CreateCommandFixture] .given() .when(Input(lotteryName = "TestLottery", amount = 200)) .expectPostPass }To test a pre-condition logic:
test("create duplicate lottery", Tag("duplicate"), ntest) { LotteryWriter().drop() grab[CreateCommandFixture] .given(LotteryCreated("TestLottery", 100)) .when(Input("TestLottery", 200)) .expectPreFail("NewLotteryName") }
Run the testcase:
[LotteryDomain] testOnly *CreateCommandTestSuite -- -n createTo retrieve the output of the command use
retrieveOutputtest("create first lottery", Tag("create"), ptest) { val commandResult = grab[CreateCommandFixture] .given() .when(Input(lotteryName = "TestLottery", amount = 200)) .expectPostPass .retrieveOutput assert(commandResult.result == "TestLottery created successfully") }
Best practice:
- Ensure there are enough unit tests i.e.
- Atleast one testcase to ensure the command does what it is supposed to do (ptest) & verify the post passes
- Atleast one testcase for every pre-condition (ntest)