// Code generated by mockery; DO NOT EDIT.// github.com/vektra/mockerypackagetestimport(mock"github.com/stretchr/testify/mock")// NewRequester creates a new instance of Requester. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.// The first argument is typically a *testing.T value.funcNewRequester(tinterface{mock.TestingTCleanup(func())})*Requester{// ...}// Requester is an autogenerated mock type for the Requester typetypeRequesterstruct{mock.Mock}typeRequester_Expecterstruct{mock*mock.Mock}func(_m*Requester)EXPECT()*Requester_Expecter{// ...}// Get provides a mock function for the type Requesterfunc(_mock*Requester)Get(pathstring)(string,error){// ...}// Requester_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get'typeRequester_Get_Callstruct{*mock.Call}// Get is a helper method to define mock.On call// - pathfunc(_e*Requester_Expecter)Get(pathinterface{},)*Requester_Get_Call{// ...}func(_c*Requester_Get_Call)Run(runfunc(pathstring))*Requester_Get_Call{// ...}func(_c*Requester_Get_Call)Return(sstring,errerror)*Requester_Get_Call{// ...}func(_c*Requester_Get_Call)RunAndReturn(runfunc(pathstring)(string,error))*Requester_Get_Call{// ...}
As you can see, this mock utilizes github.com/stretchr/testify under the hood and registers call expectations with testify. When the mock receives a call to Get(), it retrieves the expected value from testify to be returned.
This style of mock also has other interesting methods:
Run a side effect when the argument matches.
Go
funcTestRequesterMockRun(t*testing.T){m:=NewMockRequester(t)m.EXPECT().Get(mock.Anything).Return("",nil)m.EXPECT().Get(mock.Anything).Run(func(pathstring){fmt.Printf("Side effect! Argument is: %s",path)})retString,err:=m.Get("hello")assert.NoError(t,err)assert.Equal(t,retString,"")}
Run a function to perform side-effects, and return the result of the function.
Specify a path to a file that contains comments you want displayed at the top of all generated mock files. This is commonly used to display license headers at the top of your source code.
mock-build-tags
string
Set the build tags of the generated mocks. Read more about the format.
unroll-variadic
bool
If set to unroll-variadic:true, will expand the variadic argument to testify using the ... syntax. See notes for more details.
All mock objects have constructor functions. These constructors do basic test setup so that the expectations you set in the code are asserted before the test exits.
Previously something like this would need to be done:
Go
factory:=&mocks.Factory{}factory.Test(t)// so that mock does not panic when a method is unexpecteddeferfactory.AssertExpectations(t)
Instead, you may simply use the constructor:
Go
factory:=mocks.NewFactory(t)
The constructor sets up common functionalities automatically
The AssertExpectations method is registered to be called at the end of the tests via t.Cleanup() method.
The testing.TB interface is registered on the mock.Mock so that tests don't panic when a call on the mock is unexpected.
Mockery now supports an "expecter" struct, which allows your tests to use type-safe methods to generate call expectations. When enabled through the with-expecter: True mockery configuration, you can enter into the expecter interface by simply calling .EXPECT() on your mock object.
A RunAndReturn method is also available on the expecter struct that allows you to dynamically set a return value based on the input to the mock's call.
Go
requesterMock.EXPECT().Get(mock.Anything).RunAndReturn(func(pathstring)(string,error){fmt.Println(path,"was called")return("result for "+path),nil})
Note
Note that the types of the arguments on the EXPECT methods are interface{}, not the actual type of your interface. The reason for this is that you may want to pass mock.Any as an argument, which means that the argument you pass may be an arbitrary type. The types are still provided in the expecter method docstrings.
Return Value Providers can be used one of two ways. You may either define a single function with the exact same signature (number and type of input and return parameters) and pass that as a single value to Return, or you may pass multiple values to Return (one for each return parameter of the mocked function.) If you are using the second form, for each of the return values of the mocked function, Return needs a function which takes the same arguments as the mocked function, and returns one of the return values. For example, if the return argument signature of passthrough in the above example was instead (string, error) in the interface, Return would also need a second function argument to define the error value:
There might be instances where you want a mock to return different values on successive calls that provide the same arguments. For example, we might want to test this behavior:
Go
// Return "foo" on the first callgetter:=NewGetter()assert(t,"foo",getter.Get("key"))// Return "bar" on the second callassert(t,"bar",getter.Get("key"))
This can be done by using the .Once() method on the mock call expectation: