0

I have a Spock test case, in which the setup block looks like this :

setup: "set the required objects"
        def company = new Company(shortName:"infyyy",fullName:"infoysys",
            region:"tamilnadu" ,email:"a@ac.com" ,telphone:34343433,fax:34343433).save(failOnError:true)
        def project = new Project(name:"testing")
        def userInstance = new User(username:username,password:password,
            company:company,project:project,pt:["dummy"]).save(failOnError:true)
        def tasksInstance = new Tasks(title:"testingwork",startDate:(new Date()-4),endDate:(new Date().clearTime()-6),description:"blah blah",project:project,completed:true,user:userInstance).save(failOnError:true)

And more over, the Tasks domain class looks like this :

class Tasks {
    static belongsTo = [ user : User, project: Project ]
        //other code
}

And User class is like this :

class User  {
        static hasMany = [ holidays : Holiday, tasks : Tasks, pt:String, project: Project ]
        //other code
}

But when I run my test and my test fails(not with an error message, but it fails in the then block of my Spock test) and I find a error in it. My setup doesn't create any relationship between User and Tasks, which makes my test to fail.

The controller code, which I'm trying to test is :

def todaysTasks() {
        def user = User.get(springSecurityService.principal.id)
        def choice = params.managersProject
        params.max = Math.min(params.max ? params.int('max') : 10,100)
        def search = Tasks.createCriteria().list(max: params.max as Integer, offset: params.offset as Integer, order: params.order as String, sort : params.sort) {
            and {
                  project {
                      like('name',"${choice}")
                  }
                  eq('endDate', new Date().clearTime())
            }
        }
        println "todays task selected project is " + search
        [tasksInstanceList : search, tasksInstanceTotal: search.getTotalCount() ]
    }

The println in the above test prints 0. Why does this happen even though I'm making the endDate in my test less than today's date? Thanks in advance.

Ant's
  • 13,545
  • 27
  • 98
  • 148

2 Answers2

1

As far as I know, GORM does not auto-populate relationships, by following a belongsTo relationship.

I always do the following.

def u=new User(...)
u.addToTasks(
   title:"testingwork",
   startDate:(new Date()-4),
   endDate:...
)
u.save()

Note that I have not created a task object. I have passed the Map of values directly to addToX... this emphasizes that the added object belongs to User and should be instantiated and saved by GORM.

Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
  • Well, you will need to post more code so people can start to put the problem in context, because this should work – Luis Muñiz Apr 12 '12 at 10:04
  • do you mock your domain classes with @MockFor or mockDomain?.... That is what i mean by context, we don't need the controller code if you have already localized the issue being in your setup code inside your test. – Luis Muñiz Apr 13 '12 at 07:10
  • Well this is an integration test. Why do I need to mock it? – Ant's Apr 13 '12 at 09:49
  • In that case, I'm out of ideas, checking out. Good luck – Luis Muñiz Apr 13 '12 at 15:02
0

You cannot (and you shouldn't) test criteria queries in your unit tests. Criteria queires are not supported in grails (and spock) unit tests. Read this question for possible solutions.

Community
  • 1
  • 1
Tomasz Kalkosiński
  • 3,673
  • 1
  • 19
  • 25