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
Release: 2024-02-09 10:03:09
forward
779 people have browsed it

有没有办法在没有 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.
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template