【问题标题】:Grails Spock failed to create object for Mocked classGrails Spock 无法为 Mocked 类创建对象
【发布时间】:2016-07-13 16:31:46
【问题描述】:

我已经嘲笑过 Microsoft365Offer 中的 readCSV() 方法。但由于这个原因,我无法创建 Microsoft365Offer 类的对象。

GroovyMock(Microsoft365Offer, global: true)

            Microsoft365Offer.readCSV(_,_) >> {
                Microsoft365Offer microsoft365Offer =  new Microsoft365Offer(
                        "name"                  : "any thing as product name",
                        "offerURI"              : "4A-FAEA24541538",
                        "resellerPrice"         : 1.6,
                        "fob"                   : 1.41,
                        "includedQuantities"    : 0,
                        "minimumValue"          : 0,
                        "taxesPISD"             : 0,
                        "defaultTaxesISS"       : 0)

                println "=====coming in here========$microsoft365Offer"
                return ["1":microsoft365Offer ]
            }

输出=====进来========null

我该如何解决这个问题?提前致谢。

【问题讨论】:

  • 显示你的堆栈跟踪。
  • 没有堆栈垃圾,因为输出打印为空。
  • 您可能遇到了 Spock 问题 445 (github.com/spockframework/spock/issues/445)。我会尝试以不同的方式进行模拟,但没有看到有关您的测试的足够上下文来建议具体的替代方案。

标签: grails spock


【解决方案1】:

我找不到解决方案,但找到了解决问题的方法:

Microsoft365Offer microsoft365Offer =  new Microsoft365Offer(
                        "name"                  : "any thing as product name",
                        "offerURI"              : "4A-FAEA24541538",
                        "resellerPrice"         : 1.6,
                        "fob"                   : 1.41,
                        "includedQuantities"    : 0,
                        "minimumValue"          : 0,
                        "taxesPISD"             : 0,
                        "defaultTaxesISS"       : 0)

GroovyMock(Microsoft365Offer, global: true)

Microsoft365Offer.readCSV(_,_) >> {
    println "=====coming in here========$microsoft365Offer"
    return ["1":microsoft365Offer]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2014-08-11
    • 2017-03-07
    相关资源
    最近更新 更多