Home >Backend Development >Golang >Is there a way to create a new gomock controller without NewController?
When using gomock for unit testing, creating new controllers is a common requirement. However, sometimes we may not have NewController, what should we do? The answer is yes! In gomock, we can simulate a new controller by using a mock controller. This method allows us to create and use a dummy controller for testing without an actual controller instance. This way, we can test our code logic independently without being affected by external resources.
As the title states, is there a way to create a new gomock controller without NewController? Comments in the bag say
// NewController returns a new Controller. It is the preferred way to create a // Controller.
I was wondering if there is a way to create it without a constructor?
I tried many ways, such as creating it using new()
and assigning variables directly, but the controller doesn't work
Type Controller
has a private field expectedCalls
that cannot be set outside the constructor. You need to use the NewController
or WithContext
function.
The above is the detailed content of Is there a way to create a new gomock controller without NewController?. For more information, please follow other related articles on the PHP Chinese website!