Wednesday, March 19, 2008

Test in Grails

Grails provide us simplest test framework.if you want to test your function,jutst run following command:
>>grails create unit-test
then Grails will ask you which Domain you want to test.for example type project
Then Grials will generate a class in directory: gwork\gira\workspace\test\integration
Write some code like this
class ProjectTests extends GroovyTestCase {

void testSort() {

def sortByAttribute={property ->
Project.findAll().sort{it."$property"}.title
}


def titles=sortByAttribute("title")
assert titles
assert titles.size()==3
assert titles[0]=="App1"
}
void setUp(){
new Project(title:'App1',description:'This is a test',createDate:new Date()).save()
new Project(title:'App2',description:'This is a test',createDate:new Date()).save()
new Project(title:'App3',description:'This is a test',createDate:new Date()).save()
}
void tearDown(){
Project.list()*.delete()
}
}
Then run command:
>>grails run-test
You will get the test reult.

No comments: