How to test a controller that has a @Mixin base controller

I created a BaseController, which I mix with other controllers.

Example:

class BaseController () { def somemethod () { return "some method" } } @Mixin(BaseController) class MyController { def getsomething() { def test = somemethod() return test } } 

I am trying to write a test case for MyController , however it fails because it cannot find somemethod .

Currently my test is as follows

 @TestFor(MyController) class MyControllerSpec extends Specification { def "test getsomething" () { when: def m = controller.getsomething() then: response.contentAsString == "some method" } } 

But I keep getting errors like this:

 No signature of method: somemethod() is applicable for argument types: () values: [] 

Question

How can I write test test for MyController so that it finds somemethod as well

+4
source share
3 answers

Is using @TestMixin(BaseController) in the Spock test for MyController ?
Ans: - No, this is not required.

UPDATE
MyController needs a little modification. Use render instead of return . Here is the detail:

 class BaseController { def someMethod() { "Some Method" } } import grails.util.Mixin //Remember to use Grails @Mixin instead of Groovy @Mixin @Mixin(BaseController) class MyController { def getSomething() { def test = someMethod() render test } } //Unit Test @TestFor(MyController) class MyControllerUnitSpec extends Specification { void "test get something"() { when: controller.getSomething() then: response.contentAsString == "Some Method" } } //Controller Integration Test import grails.plugin.spock.ControllerSpec class MyControllerIntSpec extends ControllerSpec { void "test get something integration"() { when: controller.getSomething() then: controller.response.contentAsString == "Some Method" } } 

Notes: -
I found some testing difficulties that are listed below: -

  • The above tests passed with the initial launch. But when I changed render to return to see that my tests failed, I got compilation errors due to Grails @Mixin , which I used in MyController (two versions withFormat ). Sometimes I think this is not very good. Changing the mix to Groovy @Mixin was doing well. I did not like it. I had to stick with Grails @Mixin . Apparently and surprisingly, making the grails clean && grails compile managed to fix the problem. I was able to use Grails @Mixin correctly. I am still looking at this discrepancy.

  • If the above problem was persistent, I would consider adding a runtime mixin to the setup() method in the unit test.

how

 def setup(){ //I would not like to do the same in Integration test //Integration test should do it for me atleast. MyController.mixin BaseController } 
  • I used ControllerSpec instead of IntegrationSpec in the integration test. Injection and convention seem to be better supported in ControllerSpec for controllers. If you see, I am not creating an instance of MyContoller in the int test.

  • I have not tested it in the regular Junit Unit and Integration tests, they should also be good.

+4
source

My strong advice is not to use mixins in Grails at all . It generates a faulty bahvior in tests. You also need additional code to use these mixins for tests, which is bad.

For more information, read this replay: http://grails.1312388.n4.nabble.com/grails-2-2-2-upgrade-from-2-2-0-breaks-mixin-on-controller-tp4645461p4645466. html that relates to mixins in Grails in general or will address many of the problems reported in the commentary on this problem: http://jira.grails.org/browse/GRAILS-8652 (classes lose their mixins during unit tests).

+3
source

Based on the answers in this thread http://grails.1312388.n4.nabble.com/Testing-a-controller-that-has-a-Mixin-td4645595.html

I ended up using

 void setup() { MyController.mixin(BaseController) } 
+1
source

Source: https://habr.com/ru/post/1485133/


All Articles