我们在一个项目上有 Grails 2.2.1。我想测试一项服务。在这项服务中,我使用了一个动态查找器(findAllBy...),但是在使用域模拟的单元测试中使用这个查找器的结果是一个 EmptyList。但是当我查看 UserRole.list() 时,同一用户有 3 个 UserRoles。似乎 Domain.findAllBy... 在模拟域中被破坏了。
Domain 类如下所示:
class UserRole implements Serializable {
User user
Role role
static mapping = {
table name: "Sec_User_Sec_Role"
id composite: ['role', 'user']
version false
}
}
测试看起来像:
@TestFor(UserService)
@Mock([User, UserRole, Role])
@TestMixin([DomainClassUnitTestMixin])
class UserServiceTests {
// setup to create User, UserRole and Role
// test the service
}
服务看起来像:
class UserService {
....
private List<UserRole> findExistingUserRolePairs (final User user) {
return UserRole.findAllByUser (user)
}