rest - 如何在 grails 2.4.3 中的 Controller 中测试复合 json 对象响应

标签 rest grails testing junit spock

我正在测试 Restful Controller CustomerController 的保存方法。

客户,域类,有一个地址关联,地址有一个城市,一个城市有一个州,一个州有一个国家。

我为 Address 编写了自定义编码器和自定义绑定(bind)助手,因此 JSON 客户是这样的:

{
    "id": "g2c3e08iqton4v5bsgti33gkpd35l0er",
    "firstName": "Bob1",
    "lastName": "Patiño1",
    "middleName": "The Payaso1",
    "email": "me@bobpatino.org",
    "phoneNumber": "300555551",
    "address": {
        "addressLine1": "addressLine1",
        "postalCode": "110111",
        "city": {
            "name": "Bogotá",
            "state": "DC",
            "country": "CO"
        }
    }
}

使用该格式一切正常,记住地址的自定义编码器和绑定(bind)助手,但现在我想测试 Controller 。

 @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'   |'bob@patino.co'| '20055555'| [ addressLine1 :'addressLine1', postalCode:'110111',city :[name:'Bogotá',state:'DC',country:'CO']]
    }

我的 Controller 保存方法是

@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": "bob@patino.co",
    "address": {
        "id": null,
        "class": "models.Address"
    },
    "customerToken": "21jtdik0m99pnarth8g25meb423fmoh0",
    "lastUpdated": null,
    "gender": "M",
    "dateCreated": null,
    "class": "models.Customer",
    "firstName": "bob"
}

最佳答案

我怀疑您正在 BootStrap.groovy 中注册您的自定义 JSON 编码器,它不会针对某些类型的测试执行(我不清楚确切的规则并且没有很好的记录)

避免此限制的一种方法是使用此处描述的 ObjectMarshallerRegisterer 方法:http://mrhaki.blogspot.com/2013/11/grails-goodness-register-custom.html

关于rest - 如何在 grails 2.4.3 中的 Controller 中测试复合 json 对象响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26747428/

相关文章:

web-applications - 如何在Web应用程序中实现REST?

node.js - 如何获取 RequestLogger 的 json 响应

java - 什么可以使此 dsl 更易于键入或阅读?

ruby-on-rails - 如何以 RESTful 方式更新 has_and_belongs_to_many 集合?

json - Servant-server 的自定义 JSON 错误

grails - Grails-在不更改URL的情况下检索“注册”页面或主页

java - Windows 7 Grails 安装

javascript - 使用正确的时区解析 Grails 中的序列化日期

python - 如何制作PowerBuilder UI测试应用?

php - 通过 PUT 将文件发送到 codeigniter REST_Controller