英文:
Go use struct as interface without implementing all methods
问题
例如,我有一个包含20个方法的UserDao
接口。
type UserDao interface {
GetUser() (User, error)
GetUsers() ([]User, error)
...
}
我想为测试创建一个模拟对象,并且只使用其中一个方法。
type UserDaoMock struct { }
func (UserDaoMock) GetUser() (User, error) {
return User{}, nil
}
有没有一种方法在将UserDaoMock
作为UserDao
在测试中使用之前,不需要实现其他方法?告诉编译器这就是应该的方式?
英文:
For example i have dao with 20 methods.
type UserDao interface {
GetUser() (User, error)
GetUsers() ([]User, error)
...
}
And i want create mock for tests and use only one method.
type UserDaoMock struct { }
fucn (UserDaoMock) GetUser() (User, error) {
return User{}
}
There is a way to don't implements other methods before using UserDaoMock as a UserDao in tests? Tell the compiler that this is how it should be?
答案1
得分: 13
将UserDao
接口嵌入到你的模拟结构体中,这样它就会继承所有的方法。只实现你需要的方法(实际上会被调用的方法):
type UserDao interface {
GetUser() (User, error)
GetUsers() ([]User, error)
}
type UserDaoMock struct {
UserDao
}
func (UserDaoMock) GetUser() (User, error) {
return User{}, nil
}
测试代码:
var dao UserDao
dao = UserDaoMock{}
fmt.Println(dao.GetUser())
输出结果(在Go Playground上尝试):
{} <nil>
请注意,调用其他方法会导致恐慌,因为嵌入的UserDao
字段是nil
,所以它们背后没有真正的实现。但是UserDaoMock
确实实现了UserDao
接口,并且GetUser()
方法已经实现并可调用。
查看相关问题以检测可调用的方法:https://stackoverflow.com/questions/29988632/go-reflection-with-interface-embedded-in-struct-how-to-detect-real-functions/61448767#61448767
其他相关问题:
英文:
Embed the UserDao
interface in your mock struct, so it will have all the methods promoted. Implement only the methods you need (the methods that will actually be called):
type UserDao interface {
GetUser() (User, error)
GetUsers() ([]User, error)
}
type UserDaoMock struct {
UserDao
}
func (UserDaoMock) GetUser() (User, error) {
return User{}, nil
}
Testing it:
var dao UserDao
dao = UserDaoMock{}
fmt.Println(dao.GetUser())
Which will output (try it on the Go Playground):
{} <nil>
Note that calling any other methods would panic of course, because the embedded UserDao
field is nil
, so there is no real implementation behind them. But UserDaoMock
does implement UserDao
, and the GetUser()
method is implemented and is callable.
See related question to detect which methods are callable: https://stackoverflow.com/questions/29988632/go-reflection-with-interface-embedded-in-struct-how-to-detect-real-functions/61448767#61448767
Other related questions:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论