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

Add Mock#Reset() #384

Closed
wants to merge 1 commit into from
Closed

Conversation

jonnyreeves
Copy link

Provide a methods for clearing all programed expectations from a mock; useful during a TestSuite tear-down method to ensure you have a clean slate between test-runs.

Provide a methods for clearing all programed expectations from a mock; useful during a TestSuite tear-down method to ensure you have a clean slate between test-runs.
@codeactual
Copy link

Should all Calls and testData also be reset to fully clear the slate?

@ernesto-jimenez
Copy link
Member

@jonnyreeves could you provide an example on how you would use it?

I would like to understand the use case better :)

@jimmiebtlr
Copy link

I'll hop in with my use case.

I start a router with a passed in DB, and potentially some others. I'd like to reset the db mock between tests without needing to stop and start the server.

So pretty much any case where something can be resource or time intensive to start and stop, that you want to test with a mock.

@ernesto-jimenez
Copy link
Member

@jimmiebtlr you can already do that as follows:

// mock
type myMock struct {
    mock.Mock
}
// initialise
m := &myMock{}
// [...]
// Reset
m.Mock = mock.Mock{}

Have you ever used this?

@jimmiebtlr
Copy link

Oh, sweet, didn't realize you could do that.

@Monnoroch
Copy link

I have another use case. Here's a component:

type StuffDoer struct {
    HelpfulDependency HelpfulInterface
}

func (self *StuffDoer) DoStuff() string {
    ...
}

Of course I want to test it. I have test suit with one test per feature for the DoStuff method. Most tests want self.HelpfulDependency.On("HelpfulMethod").Return(result1), but I have a single test that wants self.HelpfulDependency.On("HelpfulMethod").Return(result2). Ideally I set up the default case in the SetupTest() and override it once in the single test that requires a different response. Right now, doing .On() for the second time does not override, so I either have to .Reset() first (which is absent) or recreate the StuffDoer object with the newly created mock. Ideally I would want testify to do what Mockito does: override previous calls with .On(), but .Reset() is an okay alternative.

@daniellowtw
Copy link

@ernesto-jimenez I also think abstracting the resetting of mocks can improve readability, and also standardize the ways of doing that. It is also more aligned with the intuition when using the mocks, as suggested in @Monnoroch example.

Using the suite package, it would be nicer to read and easier to understand

func(s *MyTestSuite) SetupTest() {
	s.myMock.Reset()
}

Instead of other possibilities such as

func(s *MyTestSuite) SetupTest() {
	// option 1
	s.myMock.ExpectedCalls = nil
	s.myMock.Calls = nil
}

or

func(s *MyTestSuite) SetupTest() {
	// option 2
	s.myMock.Mock = mock.Mock{}
}

@jvitoroc
Copy link

May I suggest a simpler solution that would require a simple modification?
https://github.com/jvitoroc/testify/blob/master/mock/mock.go#L225

func (m *Mock) Reset() {
	*m = Mock{}
}

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 this pull request may close these issues.

7 participants