grails - 单元测试

发布于 2024-11-14 21:28:29 字数 1296 浏览 2 评论 0原文

我很难对域中包含的方法进行单元测试,如下所示:

这是域类

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶情人眼里出诗心の 2024-11-21 21:28:29

这里的问题是 Grails 在运行时添加了很多动态方法。 mockDomain() 方法添加了动态 findBy... 方法等,但它没有添加executeUpdate 动态方法。

因此,您有几个选择

1/ 将单元测试移至集成测试文件夹。这样,grails 创建了一个完整的运行时环境,并将添加所有动态类。 (以较慢的运行时间为代价)

2/模拟executeUpdate的行为,以便您只是测试自己的代码。 (您不一定要对 grails 代码进行单元测试?)
例如

registerMetaClass(UserRole )
UserRole.metaClass.static.executeUpdate={String query, Map items-> println "Mocking executeUpdate"}

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.

registerMetaClass(UserRole )
UserRole.metaClass.static.executeUpdate={String query, Map items-> println "Mocking executeUpdate"}
爱给你人给你 2024-11-21 21:28:29

尝试:

newuserRole.executeUpdate 'DELETE FROM UserRole WHERE user= :user',user

看到这里

我希望它有帮助!

try:

newuserRole.executeUpdate 'DELETE FROM UserRole WHERE user= :user',user

seen here

I hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文