2

在我的集成测试中,我有@Shared从固定装置(grails 的固定装置插件)加载的字段。这些字段是夹具 bean 的别名。看看我现在的代码!

@TestFor(OrganizationUserService)
class OrganizationUserServiceSpec extends Specification {

    def fixtureLoader

    @Shared
    def fixture

    @Shared
    def activeOrganizationOne
    @Shared
    def activeChild1

    def setup() {

        fixture = fixtureLoader.load {
            build {
                activeOrganizationOne(Organization) {
                    name = "Active Organization"
                    active = true
                    parent = null
                }

                activeChild1(Organization) {
                    name = "Active Child One"
                    active = true
                    parent = activeOrganizationOne
                }

        }

        activeOrganizationOne = fixture.activeOrganizationOne
        activeChild1 = fixture.activeChild1

    }

    void "deactivateOrganization deactivates an organization and all its children"() {
        given:
            service.organizationService = new OrganizationService()
            service.userService = new UserService()


        when:
            service.deactivateOrganization(activeOrganizationOne)
            //the above method deactivates activeOrganizationOne and activeChild1
            //and activeChild1 will have a disableReason = PARENT_DEACTIVATION
        then:

            !activeOrganizationOne.active

            !activeChild1.active
            DisableReason.PARENT_DEACTIVATION.toString() == activeChild1.disableReason.toString()
    }
}

令我惊讶的是它的工作原理。服务方法通过 GORM 请求找到 activeChild1。那么@Shared测试类中的对象是如何修改的呢?

为了解释更多,deactivateOrganization让组织的孩子使用Organization.findAllByParent(org). 其中org是传递给的参数deactivateOrganization。如果deactivateOrganization只修改从 GORM 调用返回的对象,那么@shared测试中的字段如何捕获更改?我没有刷新任何东西。

我在 Grails 2.3.11 上运行 mongoDB。

我正在尝试创建一个关于如何正确集成测试的标准。所以,我想知道使用夹具和集成测试的正确方法。

提前致谢;

4

0 回答 0