0

I'm trying to test my web api using ScalaTest and I can't get the API to execute my siteData() action. The following code results in a 400, but running the same payload from the outside works just fine.

class DeviceApiSpec extends TestEnvironmentSpec with Results {

    "DeviceApi#siteData()" should {

     val api = new DeviceApi(...) // new up the controller

         "accept a valid location" in {
             val apikey = ...
             val location = Json.obj(...)
             val request = FakeRequest(POST, "/api/device")
                        .withHeaders(apikey)
                        .withHeaders("Content-Type" -> "application/json")
                        .withJsonBody(location)
             val result = api.siteData().apply(request).run()
             // do some assertions once I get it working
         }  
     }
}    

The headers and body are correct. I'm not sure that I'm setting up the test properly though, so wondering if someone can spot a hopefully obvious error.

andyczerwonka
  • 4,230
  • 5
  • 34
  • 57

1 Answers1

1

I was able to get it working. Instead of executing the api via apply().run(), I used the EssentialActionCaller.

val result = call(api.siteData(), request)
status(result) mustBe OK
contentAsJson(result) mustBe Json.obj(...)
andyczerwonka
  • 4,230
  • 5
  • 34
  • 57