2012-04-12 1 views
0

내가 setup 블록은 다음과 같습니다하는 스팍의 테스트 케이스가 있습니다Spock 테스트 케이스가 Grails에서 도메인 클래스의 관계를 만들지 않습니까?

setup: "set the required objects" 
     def company = new Company(shortName:"infyyy",fullName:"infoysys", 
      region:"tamilnadu" ,email:"[email protected]" ,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) 

그리고 이상 더는 Tasks 도메인 클래스는 다음과 같습니다

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

그리고 User 클래스는 다음과 같이이다 :

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

하지만 테스트를 실행하면 테스트가 실패합니다 (오류 메시지가 아닌 b 그것은 내 Spock 테스트의 then 블록에서 실패합니다) 오류가 있습니다. 내 설정이 UserTasks 사이의 관계를 만들지 않아 내 테스트가 실패합니다.

내가 테스트하는 데 노력하고있어 컨트롤러 코드입니다 :

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() ] 
    } 

상기 시험 인쇄 0println. 내 시험에서 endDate을 오늘 날짜보다 적게 만들지 만 왜 이런 일이 발생합니까? 미리 감사드립니다.

+0

자세한 내용을 입력해야합니다. 실패한 테스트를 게시하고 stacktrace를 사용하여 오류 메시지를 게시하십시오. –

답변

1

내가 아는 한, GORM은 belongsTo 관계를 따라 관계를 자동 채우지 않습니다.

나는 항상 다음을 수행합니다.

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

작업 개체를 만들지 않았습니다. addToX에 값의 Map을 직접 전달했습니다 ... 이것은 추가 된 객체가 User에 속하므로 GORM에 의해 인스턴스화되고 저장되어야 함을 강조합니다.

+0

심지어 이것이 작동하지 않습니다! –

+0

글쎄, 더 많은 코드를 게시해야 사람들이 문맥에 문제가 생길 수 있습니다. – loteq

+0

내 질문을 업데이트했습니다 .. –

0

단위 테스트에서 기준 쿼리를 테스트 할 수 없습니다. 기준 큐는 grails (및 spock) 유닛 테스트에서 지원되지 않습니다. 해결책을 찾으려면 this 질문을 읽으십시오.

+0

하지만 단위 테스트가 아닙니다. 통합 테스트. –

관련 문제