在我的集成测试中,我有从{fixtures}加载的@Shared
个字段(用于grails的Fixtures插件)。这些字段是夹具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()
}
}
令我惊讶的是,它有效。 activeChild1由服务方法通过GORM请求找到。那么如何修改测试类中的@Shared
对象?
为了解释更多信息,deactivateOrganization
使用Organization.findAllByParent(org)
获取组织的孩子。其中org
是传递给deactivateOrganization
的参数。如果deactivateOrganization
仅修改从GORM调用返回的对象,那么测试中的@shared
字段如何捕获更改?我没有刷新任何东西。
我正在使用Grails 2.3.11并使用mongoDB运行。
我正在尝试创建一个关于如何正确集成测试的标准。所以,我想知道使用灯具和集成测试的正确方法。
先谢谢你;