2014-11-05 38 views
2

我正在測試一個Restful控制器的保存方法,CustomerController如何在grails中的控制器中測試複合json對象響應2.4.3

客戶是領域類,具有地址關聯,地址有城市,城市有州,國家有國家。

我寫定製marshallers和Address自定義綁定幫手,讓一個JSON的客戶是這樣的:

{ 
    "id": "g2c3e08iqton4v5bsgti33gkpd35l0er", 
    "firstName": "Bob1", 
    "lastName": "Patiño1", 
    "middleName": "The Payaso1", 
    "email": "[email protected]", 
    "phoneNumber": "300555551", 
    "address": { 
     "addressLine1": "addressLine1", 
     "postalCode": "110111", 
     "city": { 
      "name": "Bogotá", 
      "state": "DC", 
      "country": "CO" 
     } 
    } 
} 

一切工作使用的格式,記住地址自定義marshallers和有約束力的幫手,但現在我想測試控制器。

@Unroll 
void "create a new customer firstName: #firstName"(){ 
     when: 'the merchantUser requests to create a new Customer' 
     controller.request.method = 'POST' 
     controller.request.contentType = JSON_CONTENT_TYPE 
     controller.request.JSON = [ 
      firstName: firstName, lastName : lastName, middleName:middleName, 
      gender: gender, email: email, phoneNumber: phoneNumber, address: address 
     ] 
     controller.save() 

     then:'The response code should be CREATED (201)' 
     controller.response.status == 201 

     and: 'The new customer should be returned' 
     controller.response.json.firstName == firstName 
     //TODO How to validate the address is added? 
     //controller.response.json.address == ???? 
     1 * controller.springSecurityService.getPrincipal() >> merchantUser 

     where: 
     firstName|lastName|middleName|gender|email|phoneNumber|address 
     JSONObject.NULL  |JSONObject.NULL |JSONObject.NULL  |JSONObject.NULL |JSONObject.NULL |JSONObject.NULL|JSONObject.NULL 
     'bob' |'patiño'|'de'  |'M' |'[email protected]'| '20055555'| [ addressLine1 :'addressLine1', postalCode:'110111',city :[name:'Bogotá',state:'DC',country:'CO']] 
    } 

我的控制器保存方法是

@Transactional 
    def save() { 
     if(handleReadOnly()) { 
      return 
     } 
     def customer = createResource() 
     customer.merchant = MerchantUser.findByUsername(springSecurityService.getPrincipal().username)?.merchant 
     customer.validate() 
     if (customer.hasErrors()) { 
      respond customer.errors, [status: UNPROCESSABLE_ENTITY] // STATUS CODE 422 
      return 
     } 

     customer.save() 

     request.withFormat { 
      json { 
       respond customer, [status: CREATED] // STATUS CODE 201 
      } 
     } 
    } 

如果我調試CustomerController,我發現客戶資源是一個有效的地址創建形成的發送信息,其實如果我運行它的代碼但是在運行測試時,controller.response.json不包含地址對象。

我也設置了客戶和地址的模擬註釋。

該問題也可能與單元測試中使用自定義編組器有關。該controller.response.json是如下

{ 
    "middleName": "de", 
    "id": 1, 
    "lastName": "patiño", 
    "phoneNumber": "20055555", 
    "merchant": { 
     "id": 1, 
     "class": "models.Merchant" 
    }, 
    "email": "[email protected]", 
    "address": { 
     "id": null, 
     "class": "models.Address" 
    }, 
    "customerToken": "21jtdik0m99pnarth8g25meb423fmoh0", 
    "lastUpdated": null, 
    "gender": "M", 
    "dateCreated": null, 
    "class": "models.Customer", 
    "firstName": "bob" 
} 

回答

1

我懷疑你在註冊您的BootStrap.groovy定製JSON編組(S),這是不是對某些類型的測試執行過程中(精確的規則都不清楚,我和無據可查),以避免此限制

一種方法是使用下面介紹的方法ObjectMarshallerRegistererhttp://mrhaki.blogspot.com/2013/11/grails-goodness-register-custom.html

+0

是的,我註冊自定義marshallers BootStrap.groovy中。我改變了你提到的文章中描述的方法,但仍然有同樣的問題。它在開發或生產中運行起來像是一種魅力,但在單元測試中,響應的地址只包含一個'{「id」:null}' 我認爲marshallers沒有工作,因爲JSON響應就像解開默認的JSON編組器。 – Neoecos 2014-11-05 21:55:41

+0

我相信你需要從一個單元切換到一個集成測試,以便在(模擬)控制器響應中拾取和使用您的JSON編組器。另外,ObjectMarshallerRegisterer方法的一個好處是它極大地簡化了單獨測試你的marshailer的任務(使用單元測試,而不是支付集成測試性能損失) – 2014-11-05 22:19:06

+0

你是對的。在看到您的評論之前,我編寫了集成測試,並且正在按照它應該的工作。你認爲這個問題仍然有效嗎?我認爲這個問題仍然有感官,因爲我沒有測試marshallers,而是測試響應內容。 – Neoecos 2014-11-06 15:52:44

相關問題