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

    }

}

这有什么问题?我该如何解决?

谢谢,

4

1 回答 1

0

我做错了这段代码:

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

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

于 2012-11-06T18:58:19.163 回答