dongzhouzhang8696 2018-11-10 14:01
浏览 33
已采纳

golang测试间谍错误地比较了相等性

I'm in the process of learning go and am adapting a Java Game of Life example from testdouble. However, the test spy I have written incorrectly compares equality of my World struct - the test passes when it should fail, since output(world) is not being called. What am I doing incorrectly?

Test:

package gameoflife

import (
    "testing"

    "github.com/google/go-cmp/cmp"
)

func TestZeroGenerations(t *testing.T) {
    generatesSeedWorldStub := GeneratesSeedWorldStub{}
    outputsWorldSpy := OutputsWorldSpy{}
    conway := NewSimulatesConway(&generatesSeedWorldStub, &outputsWorldSpy)
    seedWorld := World{}

    conway.simulate()

    correctWorld := outputsWorldSpy.wasOutputCalledWithWorld(seedWorld)
    if !correctWorld {
        t.Errorf("Output called with seed world, expected: %t, got: %t", true, correctWorld)
    }
}

type GeneratesSeedWorldStub struct{}

func (gsw *GeneratesSeedWorldStub) generate() World {
    return World{}
}

type OutputsWorldSpy struct {
    outputCalledWithWorld World
}

func (ow *OutputsWorldSpy) output(world World) {
    ow.outputCalledWithWorld = world
}

func (ow *OutputsWorldSpy) wasOutputCalledWithWorld(world World) bool {
    return cmp.Equal(world, ow.outputCalledWithWorld)
}

Implementation:

package gameoflife

type SimulatesConway struct {
    generatesSeedWorld GeneratesSeedWorld
    outputsWorld       OutputsWorld
}

func NewSimulatesConway(generatesSeedWorld GeneratesSeedWorld, outputsWorld OutputsWorld) SimulatesConway {
    return SimulatesConway{generatesSeedWorld: generatesSeedWorld, outputsWorld: outputsWorld}
}

func (sc *SimulatesConway) simulate() {
    // seedWorld := sc.generatesSeedWorld.generate()
    // sc.outputsWorld.output(seedWorld)
}

type GeneratesSeedWorld interface {
    generate() World
}

type OutputsWorld interface {
    output(world World)
}

type World struct{}
  • 写回答

1条回答 默认 最新

  • douxianji6104 2018-11-10 14:39
    关注

    When called outputsWorldSpy := OutputsWorldSpy{} golang assigned default value in outputsWorldSpy.outputCalledWithWorld = World{} and you assigned seedWorld := World{}. So they are same that's why test passed. If you want to handle that case, i suggest to use pointer.

    type OutputsWorldSpy struct {
        outputCalledWithWorld *World
    }
    
    func (ow *OutputsWorldSpy) output(world World) {
        ow.outputCalledWithWorld = &world
    }
    
    func (ow *OutputsWorldSpy) wasOutputCalledWithWorld(world World) bool {
        if ow.outputCalledWithWorld == nil {
            return false
        }
        return cmp.Equal(world, *ow.outputCalledWithWorld)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址