Reputation: 762
I am a bit new to using grails so please forgive me if this has an obvious answer. I have been going through the documentation on how unit tests are automatically created when you create a new controller. from what i have seen online and in books, the controller test class name is appended with "test" at the end. using grails 2.3.1 in the \test\unit\ directory it created StoreControllerSpec.groovy in that i have
@TestFor(StoreController)
class StoreControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void testSomething() {
\\ added to see if the test works
controller.index()
assert 'Welcome to the gTunes store!' == response.text
}
}
the problem I am having is that when running test-app it tries to run the unit test but outputs nothing and it is not marked as failed?
grails> test-app
| Running without daemon...
| Compiling 1 source files
| Compiling 1 source files.
| Running 1 unit test...
| Completed 0 unit test, 0 failed in 0m 4s
| Tests PASSED - view reports in
Upvotes: 0
Views: 948
Reputation: 276
Here you go:
grails 2.3.x uses spock framework by default for testing
//Controller class
class DomainListController {
def index() {
redirect (action:"list")
}
def list() {
render "hello"
}
}
//Test class
@TestFor(DomainListController) class DomainListControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test something"() {
}
//test method to test index() of DomainListController
def void testIndex() {
controller.index()
expect:
response.redirectedUrl == 'domainList/listll'
}
//test method to test list() of DomainListController
def void testList() {
controller.list()
expect:
response.text == "hello"
}
}
--> @TestFor mixin takes care of the contoller mocking. You can very well access some keywords here like controller, contoller.params, controller.request, controller.response without instantiating controller.
-->The response object is an instance of GrailsMockHttpServletResponse (from the package org.codehaus.groovy.grails.plugins.testing) which extends Spring's MockHttpServletResponse class and has a number of useful methods for inspecting the state of the response.
-->expect: is the expected result
Hope this helps you :) cheers - Mothi
Upvotes: 1
Reputation: 762
i managed to fix the issue by changing how the test is written eg
void "test Something"() {
controller.index()
expect:
"Welcome to the gTunes store!" == response.text
}
this is because grails now uses the spock test framework by default
Upvotes: 1
Reputation: 371
In the previous versions of grails, the class that contained the tests had to end in "Tests". Might be worth trying that?
eg.
class StoreControllerSpecTests extends Specification
Upvotes: 0