grails - 单元测试
我很难对域中包含的方法进行单元测试,如下所示:
这是域类
class UserRole implements Serializable {
User user
Role role
static void removeAll(User user) {
executeUpdate 'DELETE FROM UserRole WHERE user=:user', [user: user]
}
}
然后在服务中:
class CorporateUserService {
def delete (def cifUserInstance) {
def userDetail,users,userRole
userDetail=UserDetails.findById(cifUserInstance.userDetails.id)
users=User.findById(userDetail.user.id)
userRole=UserRole.removeAll(users)
}
}
在单元测试中:
void testDelete(){
def cifUserService = new CorporateUserService()
mockDomain(UserRole,[])
def newuserRole2=UserRole.create(user,role2)
def newuserRole=UserRole.create(user,role)
newuserRole.executeUpdate 'DELETE FROM UserRole WHERE user= :user',[user: user]
try{
cifUserInstance = cifUserService.delete(cifUser)
}
catch(RuntimeException e){
println e
}
}
我有这样的错误:
“groovy.lang.MissingMethodException: 无方法签名: com.logika.corp.security.UserRole.executeUpdate() 适用于参数类型: (java.lang.String, java.util.LinkedHashMap)值: [从 UserRole 删除,其中 user= :用户,[用户:用户1]]“
谁能知道如何修复此错误?
I have difficulty in making unit testing on the method contained in the domain like this:
This is the domain class
class UserRole implements Serializable {
User user
Role role
static void removeAll(User user) {
executeUpdate 'DELETE FROM UserRole WHERE user=:user', [user: user]
}
}
Then in the service :
class CorporateUserService {
def delete (def cifUserInstance) {
def userDetail,users,userRole
userDetail=UserDetails.findById(cifUserInstance.userDetails.id)
users=User.findById(userDetail.user.id)
userRole=UserRole.removeAll(users)
}
}
And in unit test:
void testDelete(){
def cifUserService = new CorporateUserService()
mockDomain(UserRole,[])
def newuserRole2=UserRole.create(user,role2)
def newuserRole=UserRole.create(user,role)
newuserRole.executeUpdate 'DELETE FROM UserRole WHERE user= :user',[user: user]
try{
cifUserInstance = cifUserService.delete(cifUser)
}
catch(RuntimeException e){
println e
}
}
I have an error like this :
"groovy.lang.MissingMethodException:
No signature of method:
com.logika.corp.security.UserRole.executeUpdate()
is applicable for argument types:
(java.lang.String,
java.util.LinkedHashMap) values:
[DELETE FROM UserRole WHERE user=
:user, [user:user1]]"
Can anyone know how to fix this error??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是 Grails 在运行时添加了很多动态方法。 mockDomain() 方法添加了动态 findBy... 方法等,但它没有添加executeUpdate 动态方法。
因此,您有几个选择
1/ 将单元测试移至集成测试文件夹。这样,grails 创建了一个完整的运行时环境,并将添加所有动态类。 (以较慢的运行时间为代价)
或
2/模拟executeUpdate的行为,以便您只是测试自己的代码。 (您不一定要对 grails 代码进行单元测试?)
例如
The problem here is that Grails adds a lot of dynamic methods at runtime. The mockDomain() method adds the dynamic findBy... methods etc, but it doesn't add the executeUpdate dynamic method.
So you have a couple of choices
1/ Move your unit test to the integration test folder. That way grails creates a full runtime environment and will add all the dynamic classes. (at the expense of a slower running time)
or
2/ Mock out the behaviour of the executeUpdate so that you are just testing your own code. (You don't necessarily want to unit test the grails code?)
e.g.
尝试:
看到这里
我希望它有帮助!
try:
seen here
I hope it helps!