golang test spy incorrectly comparing equality
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
AllWorld
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
go testing test-double
asked Nov 10 at 14:01
amb85
31019
31019
AllWorld
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
add a comment |
AllWorld
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
add a comment |
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)
}
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 isnil
.
– nightfury1204
Nov 10 at 15:02
add a comment |
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)
}
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 isnil
.
– nightfury1204
Nov 10 at 15:02
add a comment |
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)
}
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 isnil
.
– nightfury1204
Nov 10 at 15:02
add a comment |
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)
}
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)
}
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 isnil
.
– nightfury1204
Nov 10 at 15:02
add a comment |
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 isnil
.
– 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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