Search code examples
scalaunit-testingspecs2

Adding message in a block of Specs2 test


I am new in Specs2. I read the specs2 documentation but it's kinda confusing. I don't know if it's possible or not.

So I have Specs2 test code roughly like this:

"DataServiceTest" should {
  "InsertNewData" in {
    val name = "some name here"
    val description = "some description here"

    // Assumed DataService.insertNewData method execute the insertion
    // and returns "Data" model.
    Data data = DataService.insertNewData(name, description)

    // Checking of equality here
    data.name === name
    data.description === description

    // Assumed the "Data" model has List[SubData] property "subData"
    // and the code below is checking the List[SubData]
    data.subData.foreach {
        ...
    }

    success
  }
}

1. Is there a way to give message for these parts?

data.name === name
data.description === description

Something like "Checking data's name" in { data.name === name }. So the message will be shown on the output screen when the test is executed whether it's success or failed.

2. Is there a way to group the sub-code inside the "InsertNewData" by giving a text message like this:

"DataServiceTest" should {
  "InsertNewData" in {
    val name = "some name here"
    val description = "some description here"

    // Assumed DataService.insertNewData method execute the insertion
    // and returns "Data" model.
    Data data = DataService.insertNewData(name, description)

    "Checking basic properties of Data" in {
        // Checking of equality here
        data.name === name
        data.description === description
    }

    "Checking subData" in {
        // Assumed the "Data" model has List[SubData] property "subData"
        // and the code below is checking the subData

        data.subData must have size(3)

        data.subData.foreach {
            ...
        }

    }
    success
  }
}

UPDATE:

Based on one of answer here, I tried this:

"Checking of equality" ! e1

def e1 = {
        data.name === name
        data.description === description
        failure
}

It didn't work as it should fail. But the test result is all passed.

UPDATE #2:

I did some experiment of nested in block:

"DataServiceTest" should {
    "my test" in {

      "hello test" in {    // this block is not executed
        "hello" !== "hello"
        success
      }

      "world" === "world"
      success
    }
}

The result is success, but it should fail because of "hello" !== "hello". Looking from console screen, the is no "hello test" message, so it seems nested in block doesn't get executed.

UPDATE #3:

Based on edited answer by eric, I edited the code in Update #2:

"DataServiceTest" >> {
    "my test" >> {

        "hello test" >> {    // this block is not executed
            "hello" !== "hello"
            success
        }

        "world" === "world"
        success
    }
}

Same result, the "hello test" nested block didn't get executed.


Solution

  • An easy way to create small examples based on one "ACT" call is to use a lazy val like this:

    "DataServiceTest should" >> {
      "InsertNewData" >> {
        val name = "some name here"
        val description = "some description here"
    
        // Assumed DataService.insertNewData method execute the insertion
        // and returns "Data" model.
        lazy val data = DataService.insertNewData(name, description)
    
        "name must be ok" >> {
          data.name === name
        }
        "description must be ok" >> {
          data.description === description
        }
    
        "subdata must be ok" >> {
           data.subData.foreach {
           ...
           }
           success
        }
     }