Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

proposal: change order expectations are evaluated #246

Closed
poy opened this issue Dec 5, 2018 · 3 comments
Closed

proposal: change order expectations are evaluated #246

poy opened this issue Dec 5, 2018 · 3 comments

Comments

@poy
Copy link
Collaborator

poy commented Dec 5, 2018

Add option to NewController() (see #238 ) to enable reversing the evaluation order.

Proposal

gmock applies expectations in reverse order. This allows the following pattern:

// sut.go
type A interface {
  Foo(i int) int
}

type Something struct{
  a A
}

func NewSomething(a A) *Something {
  return Something{}
}

func (s *Something) Do(i int) {
  s.a.Foo(i)
}

// sut_test.go
func beforeEach(t *testing.T) A {
  ctrl := gomock.NewController(t)
  mockA := NewMockA()

  // Stub - Always returns 99
  mockA.
    EXPECT().
    Foo(gomock.Any()).
    Return(99).
    AnyTimes()
}

func TestFoo_OnlyDefault(t *testing.T){
  mockA := beforeEach(t)
  
  s := NewSUT(mockA)
  s.Do(5) // Mock doesn't make any assertion on value
}

func TestFoo_WithAssertion(t *testing.T){
  mockA := beforeEach(t)
  
  mockA.
    EXPECT().
    Foo(101).
    Return(99)

  s := NewSUT(mockA)
  s.Do(5) // Latest assertion will fail
}

This pattern will currently fail as the first assertion is evaluated first, as it is an AnyTimes(), it will always be successful and therefore the last assertion (the one we really care about for TestFoo_WithAssertion) won't be applied.

@poy
Copy link
Collaborator Author

poy commented Dec 5, 2018

Related to #137 #188 #7

@tocrafty
Copy link

tocrafty commented Oct 7, 2019

Agreed. This would be very usefull to inject faults to mocked objects.

@codyoss
Copy link
Member

codyoss commented Jul 10, 2020

Closing issue as I believe this is captured by #238

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants