Having the following in your controller:
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
will cause the save() action for example to set an empty flash map. I.e. the following test will fail because flash is [:] after the action returns and message is null:
Controller:
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
[...]
def save = {
flash.message = "Saved"
}
Test:
void testSave() {
controller.save()
assert null != flash.message
}
Is there any reason for that or is that a bug in Grails 2.0?
The reason flash is empty is that you have defined save() to use only POST method. That's what grails do - it doesn't allow you to access save() with GET. Your complete test should look like this:
void testSaveWithGet() {
controller.save()
assert response.status == HttpServletResponse.SC_METHOD_NOT_ALLOWED
}
void testSaveWithPost() {
request.method = "POST"
controller.save()
assert flash.message == "Saved"
}
Related
I have a test for a PersonController, it just executes save() with no params, so an invalid person is created. It should return the invalid person in the model and show the create view. But the model is empty.
The test:
import org.junit.*
import grails.test.mixin.*
#TestFor(PersonController)
#Mock(Person)
class PersonControllerTests {
...
void testSave() {
controller.save() // creates invalid person, redirects to create
assert model.personInstance != null
assert view == '/person/create'
response.reset()
populateValidParams(params)
controller.save()
assert response.redirectedUrl == '/person/show/1'
assert controller.flash.message != null
assert Person.count() == 1
}
...
}
The controller:
class PersonController {
...
def save() {
def personInstance = new Person(params)
if (!personInstance.save(flush: true)) {
render(view: "create", model: [personInstance: personInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id])
redirect(action: "show", id: personInstance.id)
}
...
}
The output:
junit.framework.AssertionFailedError: Assertion failed:
assert model.personInstance != null
| | |
[:] null false
at demographic.PersonControllerTests.testSave(PersonControllerTests.groovy:43)
How can I get the right model?
Is this the expected behavior or is this a Grails bug?
The test is failing (rightly so, I believe) because of the HTTP method restrictions in the controller, i.e. the line:
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
Set the HTTP method in the test, and the test passes:
void testSave() {
controller.request.method = 'POST'
controller.save() // creates invalid person, redirects to create
...
I have test for the save action of a controller. It just executes the action with correct params, but the problem is on the redirectedUrl line: it is null.
Using the app, after saving the domain instance, I get the redirection to the show action and the show view is rendered correctly.
Any clues of what's the problem here?
The controller:
#Transactional(readOnly = true)
class FolderController {
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
...
#Transactional
def save(Folder folderInstance) {
if (folderInstance == null) {
notFound()
return
}
if (folderInstance.ehrId)
{
def ehr = ehr.Ehr.get(folderInstance.ehrId)
ehr.directory = folderInstance
ehr.save()
}
if (folderInstance.hasErrors()) {
respond folderInstance.errors, view:'create'
return
}
folderInstance.save flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'folder.label', default: 'Folder'), folderInstance.id])
redirect folderInstance
}
'*' { respond folderInstance, [status: CREATED] }
}
}
...
}
The test:
#TestFor(FolderController)
#Mock(Folder)
class FolderControllerSpec extends Specification {
...
void "Test the save action correctly persists an instance"() {
when:"The save action is executed with a valid instance"
response.reset()
populateValidParams(params)
def folder = new Folder(params)
controller.save(folder)
println folder.errors // no errors
then:"A redirect is issued to the show action"
response.redirectedUrl == '/folder/show/1'
controller.flash.message != null
Folder.count() == 1
}
...
}
The output:
junit.framework.AssertionFailedError: Condition not satisfied:
response.redirectedUrl == '/folder/show/1'
| | |
| null false
org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse#112b2f1
at directory.FolderControllerSpec.Test the save action correctly persists an instance(FolderControllerSpec.groovy:61)
Grails scaffold controllers are smarter controllers. They respect the request format and generate the response accordingly.
For example your save action -- it redirects to the show action if the request format is form otherwise it returns saved domain instance with status CREATED.
Following code is responsible for this
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'folder.label', default: 'Folder'), folderInstance.id])
redirect folderInstance
}
'*' { respond folderInstance, [status: CREATED] }
}
And in you test cases, your request is not of form type and hence redirectedUrl is null.
To make form request, add following code in you test case before making save call--
request.format = 'form'
Hope this helps.
I forgot to add the allowedMethods field.
The first problem was that the generated tests doesn't sets the right request method for the correspondent actions, so to call .save() this is needed: controller.request.method = "POST"
Then what #user1690588 suggested (request.format = 'form') did the trick to get the right redirectedUrl.
My final test looks like this:
void "Test the save action correctly persists an instance"() {
when:"The save action is executed with a valid instance"
response.reset()
populateValidParams(params)
def folder = new Folder(params)
controller.request.method = "POST"
request.format = 'form'
controller.save(folder)
then:"A redirect is issued to the show action"
response.redirectedUrl == '/folder/show/1'
controller.flash.message != null
Folder.count() == 1
}
So, I am starting learning groovy & grails. I am trying to write unit tests for my controller like this:
void testSave() {
params.productName = 'ProdName'
params.productBarCode = '123'
params.productStore = 'ProdStore'
def response = controller.save()
assert response.productInstance.productName == 'ProdName'
}
and this is the controller action
def save() {
def productInstance = new Product(params)
if (!productInstance.save(flush: true)) {
render(view: "create", model: [productInstance: productInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id])
redirect(action: "show", id: productInstance.id)
}
and this is the exception it throws when 'test-app'
groovy.lang.MissingMethodException: No signature of method: xxx.Product.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)
at xxx.ProductController.save(ProductController.groovy:59)
at xxx.ProductControllerTests.testSave(ProductControllerTests.groovy:35)
I am sorry if this question is too naive. Please help
Thanks
Until the domain instance is mocked in the test class, it won't be able to recognize dynamic methods like save() on the domain class.
Use #Mock(Product) at class level in the test class.
I'm writing controller unit tests and I'd like to test json result when creation fails.
How can I register VndErrorJsonRenderer in unit test ? I tried simply defineBeans in setup() but it doesn't work :(
import com.vividsolutions.jts.geom.Coordinate
import grails.transaction.Transactional
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap
import static org.springframework.http.HttpStatus.CREATED
import static org.springframework.http.HttpStatus.NO_CONTENT
#Transactional(readOnly = true)
class UserController {
static namespace = "v1"
static allowedMethods = [profile: 'GET', create: "POST", update: "PUT", delete: "DELETE"]
static responseFormats = ['json', 'vnd.error+json']
def springSecurityService
def geometryFactory
/**
* Saves a resource
*/
#Transactional
def create() {
User instance = createResource(params)
instance.validate()
if (instance.hasErrors()) {
respond instance.errors, view: 'create' // STATUS CODE 422
return
}
instance.save flush: true
respond instance, [status: CREATED]
}
protected User createResource(GrailsParameterMap params) {
Double x = params.double("location.x", 0)
Double y = params.double("location.y", 0)
User user = new User()
bindData(user, params, [include: ['username', 'password', 'profile.*']])
if (x > 0 && y > 0)
user.location = geometryFactory.createPoint(new Coordinate(x, y))
else
user.location = null
user.roles = []
user.roles.add(Role.findByAuthority(Role.ROLE_USER))
return user
}
}
And my test :
#Before
void setup() {
defineBeans {
vndJsonErrorRenderer(VndErrorJsonRenderer)
}
}
void "Test the create action with a non unique username"() {
User.metaClass.encodePassword = {
"aaa"
}
// Create first user
assertNotNull getValidUser().save(flush: true)
when: "The create action is executed with a username already used"
def user = getValidUser()
controller.request.addHeader("Accept", "application/vnd.error+json,application/json")
controller.request.contentType = "application/json"
controller.request.content = JsonMapperUtil.mapAsJson(user)?.getBytes()
controller.create()
then: "The response status is UNPROCESSABLE_ENTITY and the username unique error is returned"
println response.text
response.status == UNPROCESSABLE_ENTITY.value
def json = JSON.parse(response.text)
assertNull "VND format not returned", json.errors
}
I'm using grails 2.3.6 with restful controller.
Thanks
In the case you are showing where you depend on respond it would be best to test this more as an integration test so all components that may interact with respond are all wired for you.
In a unit test for what ever beans are needed in the class under test I find it easiest to directly set them on the class under test.
I have a unit test for my UserController but since upgrading to Grails 2.0, the flash variable always seems to return an emtpy map with no message.
Here are some code snippets of the UserControllerTests:
#TestFor(UserController)
#Mock(User)
class UserControllerTests {
...
void testSaveSucceeds() {
params.userName = 'Joe'
...
controller.save()
assert null != flash.message
assert '/user/list' == response.redirectedUrl
}
}
In UserController:
def save = {
def userInstance = new User(params)
if (userInstance.validate()) {
flash.message = message(code: 'default.created.message', args: [userInstance.userName ])
...
}
But my test result is as follows:
assert null != flash.message
| | |
| [:] null
false
I have tried as an integration test as well because otherwise the response was null as weill but it did not fix the flash issue. The same problem also exists with view and model.
What am I missing? Any help highly appreciated.
Regards
Jonas
EDIT:
Here's a weird scenario:
My controller has the following:
def test = {
flash.message = "Message"
}
def save = {
flash.message = "Message"
}
My Test looks like that:
void testSaveSucceeds() {
controller.save()
println ">>> ${flash}"
controller.test()
println ">>> ${flash}"
}
The output like that:
>>> [:]
>>> [message:Message]
Interesting to mention is also that the debugger in IntelliJ stops at a breakpoint in the test() action but not in save()
HOW can that be????
Regards
Jonas
For me it means that userInstance.validate() return false ie the validation failed.