golang test spy incorrectly comparing equality











up vote
0
down vote

favorite
1












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{}









share|improve this question






















  • All World values are equal to one another. And why wouldn't they be? There's nothing in them to distinguish them. I'm not familiar with the cmp package, but native equality is defined in the spec.
    – Peter
    Nov 10 at 14:16

















up vote
0
down vote

favorite
1












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{}









share|improve this question






















  • All World values are equal to one another. And why wouldn't they be? There's nothing in them to distinguish them. I'm not familiar with the cmp package, but native equality is defined in the spec.
    – Peter
    Nov 10 at 14:16















up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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{}









share|improve this question













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{}






go testing test-double






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 14:01









amb85

31019




31019












  • All World values are equal to one another. And why wouldn't they be? There's nothing in them to distinguish them. I'm not familiar with the cmp package, but native equality is defined in the spec.
    – Peter
    Nov 10 at 14:16




















  • All World values are equal to one another. And why wouldn't they be? There's nothing in them to distinguish them. I'm not familiar with the cmp package, but native equality is defined in the spec.
    – Peter
    Nov 10 at 14:16


















All World values are equal to one another. And why wouldn't they be? There's nothing in them to distinguish them. I'm not familiar with the cmp package, but native equality is defined in the spec.
– Peter
Nov 10 at 14:16






All World values are equal to one another. And why wouldn't they be? There's nothing in them to distinguish them. I'm not familiar with the cmp package, but native equality is defined in the spec.
– Peter
Nov 10 at 14:16














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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)
}





share|improve this answer





















  • Ah, okay, so to check my understanding, if I define a struct and don't assign any value to its members, they will be assigned a default value, hence the equality comparison passing? By checking whether the memory address is nil, we guard against the case when nothing has been assigned before checking whether the two are actually equal?
    – amb85
    Nov 10 at 14:56












  • yes. For pointer golang wil assign default vault is nil.
    – nightfury1204
    Nov 10 at 15:02













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239718%2fgolang-test-spy-incorrectly-comparing-equality%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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)
}





share|improve this answer





















  • Ah, okay, so to check my understanding, if I define a struct and don't assign any value to its members, they will be assigned a default value, hence the equality comparison passing? By checking whether the memory address is nil, we guard against the case when nothing has been assigned before checking whether the two are actually equal?
    – amb85
    Nov 10 at 14:56












  • yes. For pointer golang wil assign default vault is nil.
    – nightfury1204
    Nov 10 at 15:02

















up vote
1
down vote



accepted










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)
}





share|improve this answer





















  • Ah, okay, so to check my understanding, if I define a struct and don't assign any value to its members, they will be assigned a default value, hence the equality comparison passing? By checking whether the memory address is nil, we guard against the case when nothing has been assigned before checking whether the two are actually equal?
    – amb85
    Nov 10 at 14:56












  • yes. For pointer golang wil assign default vault is nil.
    – nightfury1204
    Nov 10 at 15:02















up vote
1
down vote



accepted







up vote
1
down vote



accepted






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)
}





share|improve this answer












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)
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 14:39









nightfury1204

74026




74026












  • Ah, okay, so to check my understanding, if I define a struct and don't assign any value to its members, they will be assigned a default value, hence the equality comparison passing? By checking whether the memory address is nil, we guard against the case when nothing has been assigned before checking whether the two are actually equal?
    – amb85
    Nov 10 at 14:56












  • yes. For pointer golang wil assign default vault is nil.
    – nightfury1204
    Nov 10 at 15:02




















  • Ah, okay, so to check my understanding, if I define a struct and don't assign any value to its members, they will be assigned a default value, hence the equality comparison passing? By checking whether the memory address is nil, we guard against the case when nothing has been assigned before checking whether the two are actually equal?
    – amb85
    Nov 10 at 14:56












  • yes. For pointer golang wil assign default vault is nil.
    – nightfury1204
    Nov 10 at 15:02


















Ah, okay, so to check my understanding, if I define a struct and don't assign any value to its members, they will be assigned a default value, hence the equality comparison passing? By checking whether the memory address is nil, we guard against the case when nothing has been assigned before checking whether the two are actually equal?
– amb85
Nov 10 at 14:56






Ah, okay, so to check my understanding, if I define a struct and don't assign any value to its members, they will be assigned a default value, hence the equality comparison passing? By checking whether the memory address is nil, we guard against the case when nothing has been assigned before checking whether the two are actually equal?
– amb85
Nov 10 at 14:56














yes. For pointer golang wil assign default vault is nil.
– nightfury1204
Nov 10 at 15:02






yes. For pointer golang wil assign default vault is nil.
– nightfury1204
Nov 10 at 15:02




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239718%2fgolang-test-spy-incorrectly-comparing-equality%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues