2014-03-13 52 views
0

播放框架2.1版使用Scala,我想测试我的无效电话(缺少参数)ProductController的,我想获得错误请求响应..测试REST API控制器在Scala和游戏框架2.1

控制器代码是返回确定(Json.toJson(some class ..))或BadRequest(Json.toJson(some class ..))incase something goes wrong。

我定义的测试类:

class ProductApplicationTests extends PlaySpecification 
with Mockito 
with ProductServiceComponent 
{ 
    lazy val productService = mock[ProductService] 
    ... other things 

def app = FakeApplication(
withoutPlugins = Seq("com.typesafe.plugin.RedisPlugin"), 
withGlobal = Some(
    new GlobalSettings { 
    override def getControllerInstance[A](clazz: Class[A]) = clazz match { 
     case c if c.isAssignableFrom(classOf[ProductController]) => new ProductController(ProductApplicationTests.this).asInstanceOf[A] 
     case _ => super.getControllerInstance(clazz) 
    } 
    } 
) 
) 

def mockStuff = { 
productService.addProduct(any[AddProductRequest]) returns 
    DTOResponse(product.id.get) 

productService.updateProduct(any[UpdateProductRequest]) returns 
    DTOResponse(product.id.get) 

productService.deleteProduct(any[DeleteProductRequest]) returns 
    DTOResponse(product.id.get) 
} 
step(mockStuff) 

"Application" should { 
"Get product with no tenant Id" in {running(FakeApplication()) { 
val req = FakeRequest(method = "POST", uri = routes.ProductController.getProducts("en_US", "1", "1").url, 
       headers = FakeHeaders(
       Seq("Content-type"->Seq("application/json")) 
      ), 
       body = None 
      ) 
    val Some(result) = route(req) 

    status(result) must equalTo(400) 

问题
我得到错误:

Cannot write an instance of None.type to HTTP response. Try to define a Writeable[None.type] 

我按照这篇文章:Play 2 - Scala FakeRequest withJsonBody,我不明白什么即时做错了。 当我发送这个代码作为测试它的作品..

"Get product with valid parameters" in new WithApplication(app) { 
    val result = route(FakeRequest(GET, "/v1.0/products?lang=en_US&t=1&ids=1,2")) 
    result must beSome 
    status(result.get) must equalTo(OK) 
    contentType(result.get) must beSome.which(_ == "application/json") 
    contentAsString(result.get) must contain("products") 
    contentAsString(result.get) must contain("notFoundIds") 
} 

感谢您的任何意见/答案..

通过我的global.scala看起来像这样:

override def onBadRequest(request: RequestHeader, error: String) = { 
var errorResponse:ErrorResponse[String] = ErrorResponse(ErrorCode.GeneralError, "Error processing request", 500) 
errorResponse.addMessage(error) 
Future.successful(BadRequest(Json.toJson(errorResponse))) 
} 

override def onError(request: RequestHeader, ex: Throwable) = 
{ 
    var errorResponse:ErrorResponse[String] = ErrorResponse(ErrorCode.GeneralError, "Error processing request", 500) 
    errorResponse.addMessage(ex.getMessage) 
    Future.successful(BadRequest(Json.toJson(errorResponse))) 
} 

如果我跑获取RESTAPI测试客户端,我得到:

{"code":100,"message":"Error processing request - Missing parameter: t"} 

回答

0

如果你看看source for FakeRequest,你会注意到你需要将主体设置为AnyContentAsEmpty。在旁注中,第一个测试方法应该是POST吗?你的其他例子似乎是GET。

至于你在RESTAPI TET客户端运行Get第二个问题,错误消息似乎非常清楚,你需要提供参数t,你在前面的例子中那样val result = route(FakeRequest(GET, "/v1.0/products?lang=en_US&t=1&ids=1,2"))