mercredi 24 février 2016

How to populate domain classes in Grails 3 for unit tests

I was wondering as to how to build up a world of Domain class objects to use in my unit tests. What´s the best approach?

Say this is my code, ServiceX:

   List<Course> getAllCoursesByType(String type) {
        List<Course> list = Course.findAllByType(type)
        list
   }

This is the test for ServiceX:

 import grails.buildtestdata.mixin.Build
 import grails.test.mixin.TestFor
 import grails.test.mixin.Mock
 import spock.lang.Specification


  @TestFor(ServiceX)

    class ServiceXSpec extends Specification { 

      void "get all open courses"() {
       given:
       String type = "open"
       when:
       def list = service.getAllCoursesByType(type)

       then:
      list.size() == 4
     }

}

How can I "pre populate" the test-db (memory) sow that I actually have 4 such objects in the list?

Aucun commentaire:

Enregistrer un commentaire