سؤال

In tests i need to override function of some domain class, like this

SomeDomain.countBySomeField(Paramater)

To solve this i tried

@Mock([SomeDomain])
class SomeDomainTests ...
...
    void test() {
        SomeDomain.metaClass.static.countBySomeField = { -> 1}
    }

But when this method invoke in controller(that was invoked by test), what i expet not happen. Probably, you say go read documentation, but i didnt find someting that will tell me how to do this. I will be grateful for any article or example that can say me where im wrong.

هل كانت مفيدة؟

المحلول

I solve my problem by doing this

@TestFor(SomeController)
@Mock([SomeDomain])
@TestMixin(DomainClassUnitTestMixin)
class SomeControllerTests {
    ...
    void test() {
        SomeDomain.metaClass.'static'.countBySomeParam = { a -> 0}
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top