如何测试在 withCriteria(Spock) 中包含 resultTransformer 的 grail 服务?

How to test a grail service that has resultTransformer within withCriteria(Spock).?

下面class,我只需要RevShareFormula.withCriteria到return一个结果, 但在 resultTransformer() 方法中出现异常。

谁能告诉我如何模拟下面的方法,以便我从 withCriteria

得到一些结果

这里是 class:

class PartnerFinancialService {

def getPartnerPayeeRevenuShareDetails(long partnerPayeeId, def contextTypeCode) {

def partnerPayeesRevShareFormula = RevShareFormula.withCriteria {
  resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
  createAlias('partnerRevShareConfig', 'partnerRevShareConfig')
  createAlias('pricingModel', 'pricingModel')
  createAlias('partnerRevShareConfig.revshareCategory', 'revshareCategory')
  and {
    eq("revshareCategory.payeeProfileId", partnerPayeeId)
    eq("revshareCategory.referenceContextTypeCode", contextTypeCode)
    isNull("partnerRevShareConfig.revshareValidToDate")
  }

  projections {
    property("id", "formulaId")
    property("pricingModel.id", "pricingModelId")
    property("pricingModel.pricingName", "pricingName")
    property("pricingModel.pricingType", "pricingType")
    ..
    ..
   }
}    
}   

这里是测试class

@TestFor(PartnerFinancialService)
@Mock(RevShareFormula)
class PartnerFinancialServiceSpec extends Specification {

void "test getPartnerPayeeRevShareDetails"() {
def partnerPayeeRevShare = new PartnerRevShareConfig()
    partnerPayeeRevShare.id = 1
    def revShareModel = new PricingModel();
    revShareModel.id = 1
    def partnerPayeeRevShareFormula = new RevShareFormula();
    partnerPayeeRevShareFormula.id=5
    partnerPayeeRevShareFormula.pricingModel = revShareModel
    partnerPayeeRevShareFormula.partnerRevShareConfig = partnerPayeeRevShare
    partnerPayeeRevShareFormula.revshareFormula = "revshare*10"
    partnerPayeeRevShareFormula.revshareTierHighValue = 0
    partnerPayeeRevShareFormula.revshareTierLowValue= 0 

    RevShareFormula.metaClass.static.withCriteria = {partnerPayeeRevShareFormula}

    when:
    def result = service.getPartnerPayeeRevenuShareDetails(1,"PKG")
    then:
    //assert result.pricingModel.id == 1
    println "Succesfully Fetched from DB"
} 
}

得到以下异常。

<testcase classname="com.orbitz.dat.partners.PartnerFinancialServiceSpec" name="test getPartnerPayeeRevShareDetails" time="0.039">
<error message="No signature of method: com.orbitz.dat.partners.PartnerFinancialService.resultTransformer() is applicable for argument types: (org.hibernate.transform.AliasToEntityMapResultTransformer) values: [org.hibernate.transform.AliasToEntityMapResultTransformer@3632aa4]" type="groovy.lang.MissingMethodException">groovy.lang.MissingMethodException: No signature of method: com.orbitz.dat.partners.PartnerFinancialService.resultTransformer() is applicable for argument types: (org.hibernate.transform.AliasToEntityMapResultTransformer) values: [org.hibernate.transform.AliasToEntityMapResultTransformer@3632aa4]
    at com.orbitz.dat.partners.PartnerFinancialService.$tt__getPartnerPayeeRevenuShareDetails_closure24(PartnerFinancialService.groovy:39)
    at grails.gorm.CriteriaBuilder.invokeClosureNode(CriteriaBuilder.java:1093)
    at grails.gorm.CriteriaBuilder.invokeMethod(CriteriaBuilder.java:314)
    at org.grails.datastore.gorm.GormStaticApi.withCriteria_closure11(GormStaticApi.groovy:304)
    at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302)
    at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:37)
    at org.grails.datastore.gorm.GormStaticApi.withCriteria(GormStaticApi.groovy:303)
    at com.orbitz.dat.partners.PartnerFinancialService.$tt__getPartnerPayeeRevenuShareDetails(PartnerFinancialService.groovy:38)
    at com.orbitz.dat.partners.PartnerFinancialServiceSpec.test getPartnerPayeeRevShareDetails(PartnerFinancialServiceSpec.groovy:71)

使用集成测试。永远不要用单元测试来测试 ORM 代码。我知道它比较慢,体验也不那么愉快,但是如果你认为你实际上是在用这个测试测试与数据库查询相关的东西,那你就是在自欺欺人。您正在测试 grails 的测试框架(内存中的 GORM 实现)