简体   繁体   中英

Testing a PlayFramework (2.5.x) Web API using ScalaTest

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.

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(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM