2016-07-28 72 views
0

Spock不執行我的spock測試之一是跳過when子句並右移到then子句。當條款

@Unroll 
void "test refresh metadata with #consumerNames"() { 
    setup: 
    consumerNames.each { 
     new CredentialConsumer(name: it).save(validate: false) 
    } 

    when: 
    service.refreshMetadata() 

    then: 
    consumerNames.each { consumerName -> 
     Metadata metadata = new Metadata(content: "Metadata for $consumerName") 
     ConsumerDefinition consumerDefinition = new ConsumerDefinition(name: consumerName, metadata: metadata) 
     1 * mockApplicationLibraryService.fetchConsumerDefinition(consumerName) >> consumerDefinition 
     assert CredentialConsumer.findByName(consumerName)?.metadata?.content == metadata.content 
    } 

    where: 
    consumerNames  | _ 
    ["bacon"]   | _ 
    ["bacon", "eggs"] | _ 

} 

我得到下面的結果...

Condition not satisfied: 

CredentialConsumer.findByName(consumerName)?.metadata?.content == metadata.content 
        |   |    |   |  | |  | 
        |   bacon   null  null | |  Metadata for bacon 
        |           | [email protected]77 
        |           false 
        com.datapriviasoftware.completesso.CredentialConsumer : 1 

Expected :Metadata for bacon 

Actual :null 

當我通過它一步與調試器,它執行setup條款,跳過when條款,並立即執行then條款。

回答

0

因此,我知道Spock在執行when子句之前使用AST轉換來處理then子句中的模擬。當我想通過它時,我意識到處理then子句中的模擬需要執行整個each循環,所以我需要將顯式斷言與模擬定義分開。

這工作...

@Unroll 
void "test refresh metadata with #consumerNames"() { 
    setup: 
    consumerNames.each { 
     new CredentialConsumer(name: it).save(validate: false) 
    } 

    when: 
    service.refreshMetadata() 

    then: 
    consumerNames.each { consumerName -> 
     Metadata metadata = new Metadata(content: "Metadata for $consumerName") 
     ConsumerDefinition consumerDefinition = new ConsumerDefinition(name: consumerName, metadata: metadata) 
     1 * mockApplicationLibraryService.fetchConsumerDefinition(consumerName) >> consumerDefinition 
    } 

    and: 
    consumerNames.each { consumerName -> 
     assert CredentialConsumer.findByName(consumerName)?.metadata?.content == "Metadata for $consumerName" 
    } 

    where: 
    consumerNames  | _ 
    ["bacon"]   | _ 
    ["bacon", "eggs"] | _ 

}