Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.7.3 call AssertExpectations without On cause panic #1206

Closed
peczenyj opened this issue Jun 20, 2022 · 0 comments · Fixed by #1207
Closed

Release 1.7.3 call AssertExpectations without On cause panic #1206

peczenyj opened this issue Jun 20, 2022 · 0 comments · Fixed by #1207

Comments

@peczenyj
Copy link

If I call call method AssertExpectations without having call the method On it panics

panic: runtime error: invalid memory address or nil pointer dereference

I usually have a test structure like the one below: not always I will call mock.On to prepare the mock - depends on the case and the preconditions. But I always check if the expectations are correct.

On Release 1.7.3 the mutex field in the Mock struct became a pointer to a mutex. And some methods check if the mutex is nil and initialize, but it is not the case of AssertExpectations. In fact this release break all of my test suites.

I note that there is a Test method that can initialize it, but I never use it. And the documentation says the test field is optional. Call Test seems vague and redundant (what is test? it tests my mock? it set a field to be used?), since I still need to call AssertExpectations.

What is the solution? rewrite my test suites or may I submit a patch to initialize the mock? or perhaps I should create a constructor for my mocks that calls mock.Test and prepare it ?

func TestXxx(t *testing.T) { 
  testcases := []struct {
    label string
    prepare func(*myMockObject)
    ...  
  }{ ... }
 
  for _, tc := range testcases {
    tc := tc
    t.Run(tc.label, func(t *testing.T) {
         m := &myMockObject{}
         defer m.AssertExpectations(t)

         tc.prepare(m)

         // rest of the test
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant