# How to test a command?

  1. Open the respective command test suite, ex: CreateCommandTestSuite.scala

  2. Grab the CommandFixture, provide the Input (if any) & verify the post & pre conditions.

    For example:

    1. 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
      }
    
    1. 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")
        }
      
  3. Run the testcase:

    [LotteryDomain] testOnly *CreateCommandTestSuite -- -n create
    
  4. To retrieve the output of the command use retrieveOutput






     



    test("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)