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

proposal: change order expectations are evaluated #246

Closed
@poy

Description

@poy

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions