覆盖Grails GORM域类上的事件关闭以进行单元测试 [英] Overriding event closure on Grails GORM domain class for unit testing

查看:80
本文介绍了覆盖Grails GORM域类上的事件关闭以进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个新的Grails项目,最近注意到Spring Security Core生成的User类中的默认约定现在通过beforeInsert/Update事件自动对密码进行编码.这是一种很好的,干净的,DRY的编码方式,也使人们无法忘记这样做.

I'm working on a fresh Grails project and recently noticed the default convention in the Spring Security Core generated User class now auto-encodes the password via a beforeInsert/Update event. That's a nice, clean, DRY way of doing the encode, and also makes it impossible to forget to do so.

但是,现在,当尝试编写一些使用所述User类的单元测试时,我发现我要么必须模拟出springSecurityService(由于编码),要么更优选(而且很干净),我会只需用一个不执行任何操作的方法覆盖beforeInsert/Update闭包即可.通常,在Groovy中,可以使用ExpandoMetaClass覆盖一种方法,等等.

However, now when trying to write up some unit tests which make use of said User class, I find I either have to mock out the springSecurityService (due to the encode), or more preferably (and cleanly), I'd just override the beforeInsert/Update closure with one that does nothing. Typically in Groovy one can override a method using the ExpandoMetaClass, ala...

User.metaClass.beforeInsert = { /* do nothing */ }  

...但是我发现原来的beforeInsert在创建和保存新用户时继续被调用.这又导致我的单元测试崩溃.仅解决此问题并模拟该服务对我来说是微不足道的,但是上面的 应能工作 .我想念什么吗? GORM的事件关闭是否有其他我没有提起的事情?

...but I'm finding that the original beforeInsert continues to be called upon creating and saving a new User. This in turn causes my unit tests to blow up. It's trivial for me to just work around this and mock out the service, but the above should work. Am I missing something? Is there something different with GORM's event closures that I'm not picking up on?

推荐答案

为了提高性能,Grails直接使用反射和缓存方法句柄来调用事件,而不是Groovy的meta层.原因是,如果要保存数百个域实例,那么如果Grails必须为每个事件都要通过Groovy的元层,那么这将严重损害性能.

In order to improve performance Grails uses reflection directly with caches method handles for invoking events and not Groovy's meta layer. The reason is that if you are saving hundreds of domain instances it can seriously hurt performance if Grails had to go through Groovy's meta layer for each event.

可以通过以下方法来解决此问题,例如定义自己的User类,以根据测试设置的系统/环境属性来禁用事件,但目前尚无办法通过元编程来覆盖这些方法.

There are ways around this such as defining your own User class that disables the event based on a system/environment property that your test sets etc. but there is currently no way to override the methods via meta programming.

这篇关于覆盖Grails GORM域类上的事件关闭以进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆