Grails:单元测试域类约束给出了错误的结果

时间:2012-11-06 18:16:31

标签: unit-testing grails junit grails-2.0

我正在尝试为我的域类编写单元测试来测试约束但是我得到了意想不到的结果。这是我的域类:

class Student {
    String firstName
    String lastName

    static constraints = {
        firstName blank:false,minSize:2,maxSize:20,validator: { val, obj ->
                      def similarUser = Student.findByFirstNameIlikeAndLastNameIlike(obj.firstName, obj.lastName)
                      if(!similarUser || obj.id == similarUser.id){
                      return ['Student.invalid.unique.message']
                      }
                    }
        lastName blank:false, minSize:2,maxSize:20
    }

}

这是我的单元测试:

@TestFor(Student)
@Mock(Student)
class StudentTests {
    def st

    @Before
    void setUp(){       
        mockForConstraintsTests(Student)        
        st=new Student(firstName:"FerasO",lastName:"Ahmad")     

    }


    @Test
    void testMinSize() {                
        assertTrue student.validate() // I guess this should return true but It returns false

    }

}

这出了什么问题?我怎么能解决它?

谢谢,

1 个答案:

答案 0 :(得分:0)

我做错了这段代码:

if(!similarUser || obj.id == similarUser.id)

应为if(similarUser && obj.id == similarUser.id)