Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing SPOCK-所有@Shared变量都为空_Unit Testing_Grails_Spock - Fatal编程技术网

Unit testing SPOCK-所有@Shared变量都为空

Unit testing SPOCK-所有@Shared变量都为空,unit-testing,grails,spock,Unit Testing,Grails,Spock,这是我的测试课: import grails.test.mixin.* import spock.lang.Specification import spock.lang.Unroll import spock.lang.* import groovy.util.logging.Slf4j @Slf4j @Mock([PromoRule, PromoCode, SecUser]) @TestFor(PromoService) class PromoServiceSpec extends Spe

这是我的测试课:

import grails.test.mixin.*
import spock.lang.Specification
import spock.lang.Unroll
import spock.lang.*
import groovy.util.logging.Slf4j

@Slf4j
@Mock([PromoRule, PromoCode, SecUser])
@TestFor(PromoService)
class PromoServiceSpec extends Specification {

@Shared testUser
@Shared testCode
@Shared testRule

def setup() {

}

@Unroll
def 'valid promo codes - #user, #code'() {
    given:
    testRule = new PromoRule(
            name : "ZTEST",
            receiverAmount : 5,
            receiverAmountType : PromoRule.AmountType.DOLLARS,
            senderAmount : 0,
            senderAmountType : PromoRule.AmountType.DOLLARS,
            receiverPointsAmount : null,
            receiverPointsAmountType : null,
            receiverMaxUse : null,
    )
    testRule.save(flush:true, failOnError:true)

    testUser = new SecUser(
            id: 1,
            version: 0,
            accountExpired: false,
            accountLocked: false,
            age: 9000,
            balance: 100,
            dateCreated: new Date(),
            emailVerified: true,
            enabled: true,
            firstName: 'Sir',
            lastName: 'Buttocks',
            lastUpdated: new Date(),
            lockedBalance: 0,
            username: "1",
            staff: false,
            displayName: 'sir_buttocks',
            usernameChosen: true,
            depositMade: true,
            depositOfferRecentlySeen: false,
            pin: null
    )
    testUser.save(flush: true, failOnError: true)

    testCode = new PromoCode(
            rule : testRule,
            code : "3",
            senderId : 1,
    )
    testCode.save(flush:true, failOnError:true)

    expect:
    service.isValidPromoCode(user, code) == value

    where:
    user | code || value
    testUser | testCode || true
}
}

当我运行此测试时,我得到以下结果:

| Failure:  valid promo codes - null, null(skillz.PromoServiceSpec)
|  Condition not satisfied:

service.isValidPromoCode(user, code) == value
|       |                |     |     |  |
|       false            null  null  |  true
skillz.PromoService@20e0e9d5         false
我尝试了大量不同的配置和布局,它们要么给我一个空指针异常(指向变量本身),要么给变量一个空值

静态
变量中执行所有定义也不会改变任何东西,与使用
@Shared
的结果相同

我也尝试过模拟这些,但在尝试为类执行.Mock()时,总是会出现null异常


谢谢

我不确定您在这里想要实现什么,但是在第一次输入方法之前(剩余部分),会对
where
块进行计算,此时,您的共享变量为null。您必须提前设置它们,例如在
setupSpec
(而不是
setup
)方法中。

您是否也尝试过
@Shared def testUser
?使用
def
。是的,我不喜欢这样做。