[英]Migrating from Grails 1.x to 2.3.9 and fixing Spock tests to run in the process
I have a question about Spock testing the list() method in a controller. 我有一个关于Spock在控制器中测试list()方法的问题。 I am also migrating from Grails 1.x to Grails 2.3.9.
我也从Grails 1.x迁移到Grails 2.3.9。 The question is how do I get the mock object I am creating visible to the controller so that when I call list() on it it sees the mock object.
问题是如何使正在创建的模拟对象对控制器可见,以便当我在其上调用list()时看到模拟对象。
Here is the code from the controller under test: 这是来自被测控制器的代码:
class XxCatalogFormController {
def list() {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[dpCatalogFormInstanceList: XxCatalogForm.list(), dpCatalogFormInstanceTotal: XxCatalogForm.count()]
}
Here is the Spock test I have: 这是我的Spock测试:
@TestFor(XpCatalogFormController)
@Mock([XxCatalogForm, DpCatalog])
@TestMixin(GrailsUnitTestMixin)
class XpCatalogFormControllerSpec extends Specification {
def 'list action: 1 dpCatalogForm'() {
setup:
mockDomain(XxCatalogForm, [dpCatalogFormInstance])
mockDomain(DpCatalog, [catalog])
params.max = 1
when:
expect:
controller.list() == [dpCatalogFormInstanceList: [dpCatalogFormInstance], dpCatalogFormInstanceTotal: 1]
where:
catalog = new DpCatalog(name: 'TestCatalog')
dpCatalogFormInstance = new XxCatalogForm(url: 'catalog_testForm.gsp',
catalog: catalog, confirmMessage: 'test', introBannerUrl: '/site/test.gsp',
successUrl: 'test.gsp', name: 'test')
}
And here is the test result: 这是测试结果:
Condition not satisfied: controller.list() == [dpCatalogFormInstanceList: [dpCatalogFormInstance], dpCatalogFormInstanceTotal: 1] | | | | | | false com.kpi.dp.catalog.XxCatalogForm : (unsaved) | [dpCatalogFormInstanceList:[], dpCatalogFormInstanceTotal:0] com.kpi.dp.catalog.XxCatalogFormController@784f6502
junit.framework.AssertionFailedError: Condition not satisfied:
controller.list() == [dpCatalogFormInstanceList: [dpCatalogFormInstance], dpCatalogFormInstanceTotal: 1]
| | | |
| | false com.kpi.dp.catalog.XxCatalogForm : (unsaved)
| [dpCatalogFormInstanceList:[], dpCatalogFormInstanceTotal:0]
com.kpi.dp.catalog.XxCatalogFormController@784f6502
at com.kpi.dp.catalog.XxCatalogFormControllerSpec.list action: 1 dpCatalogForm(XxCatalogFormControllerSpec.groovy:64)
Unable to confirm which mixin is used in the test class, modifying the test class to use @Mock
should work. 无法确认测试类中使用了哪种mixin,应修改测试类以使用
@Mock
。 Use @Mock
instead of mockDomain()
. 使用
@Mock
而不是mockDomain()
。 Expecting the class to be under test/unit
. 期望班级正在接受
test/unit
。
import grails.test.mixin.*
import spock.lang.Specification
@TestFor(XxCatalogFormController)
@Mock([DpCatalog, XxCatalogForm])
class XxCatalogFormControllerSpec extends Specification {
def 'list action: 1 dpCatalogForm'() {
given:
def catalog = new DpCatalog(name: 'TestCatalog')
def dpCatalogFormInstance = new XxCatalogForm(
url: 'catalog_testForm.gsp',
catalog: catalog,
confirmMessage: 'test',
introBannerUrl: '/site/test.gsp',
successUrl: 'test.gsp', name: 'test'
)
and:
params.max = 1
expect:
controller.list() == [
dpCatalogFormInstanceList: [dpCatalogFormInstance],
dpCatalogFormInstanceTotal: 1
]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.