Home >Backend Development >Golang >Is there a way to create a new gomock controller without NewController?

Is there a way to create a new gomock controller without NewController?

WBOY
WBOYforward
2024-02-09 10:03:09772browse

有没有办法在没有 NewController 的情况下创建新的 gomock 控制器?

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.

Question content

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

Workaround

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete