Grails单元测试

时间:2014-03-13 21:23:49

标签: unit-testing grails

我目前正在尝试为grails应用程序编写一些单元测试。当我运行测试时,我得到了

    Compilation error compiling [unit] tests: startup failed:
    C:\Users\adam\Dropbox\NetBeansProjects\Assignment\test\unit\Assignment\FixtureControllerTests.
    groovy: 20: Only classes and closures can be used for attribute 'value' in  
    @grails.test.mixin.TestMixin
      @ line 20, column 12.
       @TestMixin(GrailsUnitTestMixin)

我无法弄清楚为什么会收到此错误。 我的代码是:

    package Assignment

    import grails.test.mixin.Mock
    import grails.test.mixin.TestFor
    import grails.test.mixin.TestMixin

    @TestFor(FixtureController)
    @Mock(Fixture)
    @TestMixin(GrailsUnitTestMixin)


    class FixtureControllerTests {

        @Test
        public void testAddingFixture() {

            params.hometeamid = 1
            params.awayteamid = 2
            params.linkCount = 1
            params.link0 = "http://www.google.com"
            params.name0 = "Google"
            params.place = "Rotherham"
            params.date = new Date()
            controller.createFixture()

       }
   }

1 个答案:

答案 0 :(得分:1)

这就是测试的样子:

package assignment //considering lowercase for package name

import grails.test.mixin.Mock
import grails.test.mixin.TestFor

@TestFor(FixtureController)
@Mock(Fixture) //Assuming Fixture is a domain object you are working with
class FixtureControllerTests {

    void testAddingFixture() {
        params.hometeamid = 1
        params.awayteamid = 2
        params.linkCount = 1
        params.link0 = "http://www.google.com"
        params.name0 = "Google"
        params.place = "Rotherham"
        params.date = new Date()

        controller.createFixture()

        //assert something
    }
}

我相信你使用的是Grails 2.2。*或更少版本,而那些版本中用于测试的模板有TestMixin,后来在较新版本中被TestFor取代。最新版本默认使用spock。