Grails控制器在集成测试中未返回JSON
我已经定义了一个控制器:Grails控制器在集成测试中未返回JSON,json,grails,groovy,spock,Json,Grails,Groovy,Spock,我已经定义了一个控制器: class FamilyController extends RestfulController { def springSecurityService def familyService static responseFormats = ['json'] FamilyController() { super(Family) } @Override protected List<Fami
class FamilyController extends RestfulController {
def springSecurityService
def familyService
static responseFormats = ['json']
FamilyController() {
super(Family)
}
@Override
protected List<Family> listAllResources(Map params) {
def user = springSecurityService.loadCurrentUser()
return Family.findAllByUser(user)
}
@Override
protected Family createResource() {
//def instance = super.createResource()
//TODO: Should be able to run the above line but there is an issue GRAILS-10411 that prevents it.
// Code from parent is below, as soon as the jira is fixed, remove the following lines:
def p1 = params
def p2 = getObjectToBind()
Family instance = resource.newInstance()
bindData instance, getObjectToBind()
/*
* In unit testing, the bindData operation correctly binds the params
* data to instance properties. When running with integration tests,
* this doesn't happen and I have to do the property assignment myself.
* Not sure if this breaks anything elsewhere.
*/
instance.properties = p1
//Code from super ends here
familyService.addFamilyToUser(instance)
return instance
}
@Override
protected Family queryForResource(Serializable id) {
def inst = familyService.safeGetFamily(Long.parseLong(id))
return inst
}
public create()
{
Family r = this.createResource()
render r as JSON
}
public index()
{
render this.listAllResources(params) as JSON
}
}
因此,在第一个when块中,我没有列出任何族(以及响应中的空json对象和[]字符串),这是我所期望的,但直到我在代码中实际实现了index操作,我才从该操作中得到我所期望的json
在2号的时候。我创建了一个族(数据绑定有问题,但我加入了一个解决方案,首先要做的事情),然后创建并保存了该族,但响应中没有返回任何内容。我使用了调试器,看到JSON封送器为新创建的族触发,所以有东西正在呈现JSON,但它要去哪里
因此,我显然做错了什么(或者我不知道从传递给web客户端的返回内容来看,操作应该做什么),但我是这个领域的新手,对做错了什么一无所知。集成测试文档显示,与其使用提供的“控制器”,不如使用,您应该实例化自己的Instance(例如def fc=new FamilyController()),并调用自动添加的响应。我不确定这是否有帮助?此外,在解析JSON时,我们使用了controllerInstance.response.contentAsString,因此也值得一试?对于集成测试,您不应该模仿任何东西。您可以指定grails版本吗?看起来您正在使用Grails2.X
@Mock([AdultPlanning, Primary, Secondary, Child, Family, Tenant, User])
class FamilyControllerIntegrationSpec extends IntegrationSpec {
Tenant tenant
User user
FamilyController controller = new FamilyController()
FamilyService familyService = new FamilyService()
def setupSpec() {
tenant = new Tenant(name: 'MMSS')
user = new User(subject: "What is a subject?", identityProvider: "http://google.com/")
tenant.addToUsers(user)
tenant.save()
user.save() // The tenant save should propagate to the user, this is to make sure.
/*
* Set up the custom marshalling code for family objects.
*/
new AdultPlanningMarshaller()
new ChildMarshaller()
new FamilyMarshaller()
new FamilyTypeEnumMarshaller()
return
}
def cleanup() {
}
void "Create a Family"() {
when:
def mSpringSecurityService = mockFor(SpringSecurityService)
/*
* I expect to make n calls (1..3) on loadCurrentUser in this test.
*/
mSpringSecurityService.demand.loadCurrentUser(1..3) { -> return user }
controller.springSecurityService = mSpringSecurityService.createMock()
controller.request.contentType = 'text/json'
controller.index()
def json
try {
json = controller.response.json
}
catch (e)
{
json = null // action didn't emit any json
}
def text = controller.response.text
then:
!json
text == "[]"
when:
controller.params.name = "Test"
controller.params.typeOfFamily = FamilyTypeEnum.SINGLE
controller.familyService.springSecurityService = controller.springSecurityService //mSpringSecurityService.createMock()
/*
* I've tried both GET (default) and POST to get bindData to pick up parameters properly,
* with no joy.
*/
// controller.request.method = "POST"
controller.request.contentType = 'text/json'
controller.create()
try {
json = controller.response.json
}
catch (e)
{
json = null // action didn't emit any json
}
text = controller.response.text
then:
1 == 1
when:
controller.index()
json = controller.response.json
then:
json.name == "Test"
}
}