为什么我们使用接口作为模拟方法Golang

如何解决为什么我们使用接口作为模拟方法Golang

我是Golang的新手,一直在探索,但不清楚单元测试中的模拟。任何人都可以解释以下特定问题吗?

Question1 :为了用Golang编写单元测试,为什么我们需要接口来模拟方法,为什么不仅要使用struct?

问题2 :为什么我们将接口注入struct(我们称之为外部方法)

使用结构-

type GlobalData struct {}

var (
    GlobalObj = GlobalData{}
)

func (g GlobalData) GetGlobalData(a string) string{
    return a
}

具有接口定义-

type GlobalInterface interface {
    GetGlobalData(a string) string
}

type GlobalData struct {}

var (
    GlobalObj = GlobalData{}
)

func (g GlobalData) GetGlobalData(a string) string{
    return a
}

谢谢

解决方法

问题1 :为了在Golang中编写单元测试,为什么我们需要接口来模拟方法,为什么不仅要使用struct?

答案:它不是强制性的

问题2 :为什么我们将接口注入struct(我们称之为外部方法)

答案:因为,它可以帮助您替换实际的函数调用(这可能会触发一些超出范围的操作,作为单元测试的一部分,例如数据库调用某些API调用) 等),方法是注入MockStruct(将实现与实际代码中相同的interface)。 多态,简而言之。

因此,您创建一个MockStruct并为其定义自己的mockMethods。作为多态,您的单元测试选择MockStruct不会抱怨。呼叫实际的数据库端点或http端点不属于单元测试

仅供参考,我可以将您指向我的github代码库之一,其中我为small test case编写了a file。如您所见,我很嘲笑:

  1. GuestCartHandler interface,这使我无法打电话给actual implementation
  2. 使用"github.com/DATA-DOG/go-sqlmock"软件包模拟sql connection。这有助于我避免建立实际的db client(因此,在进行单元测试时不依赖数据库)

请让我知道您是从概念上获得创意还是需要进一步澄清。

,

如果您在包用户中有关于类型的方法,比如说。 套餐用户

 selected_fields = ['Player bio','team']

现在在目录包中导入:

type User struct {
 name string
}

func (u *User) GetUserProfile() UserProfile{}

现在要测试getUserCatalog方法有两种方法:

package catalog

import user

func getUserCatalog(user user.User) []catalog {
 user.GetUserProfile()
}

使用这种方法可以在测试运行时轻松通过模拟,例如:

1. var getUserProfileFunc = user.GetUserProfile

这是测试它的最简单方法。

现在有另一种使用界面的方式,在包用户中添加类似的界面

getUserProfile = func() UserProfile { 
 return fakeUserProfile 
}

如果用户包是您无法控制的库,则创建您自己的界面,键入并使用它。

在这种情况下,目录包中的测试将变为:

因为现在将从UserInterface类型而不是从UserType调用方法,因此在测试时:

type UserInterface interface {
  GetUserProfile() UserProfile
}

并按照以下步骤操作

UserInterface = fakeUserStruct

现在运行测试时:

//1. define type of func to return 

type typeGetUserProfile func() UserProfile

//2. create a var to return

var mockedGetUserProfile typeGetUserProfile

//3. create a type

type FakeUser struct{}

//4. implement method interface

func (user *FakeUserStruct) GetUserProfile() UserProfile{
  return mockedGetUserProfile
 }

有一个模拟库,可以帮助创建用于模拟的样板代码。选中此https://github.com/stretchr/testify

还有许多其他的模拟库,但是我使用过这个库,真的很棒。

我希望这会有所帮助。

如果不能,请告诉我,我将给出一些示例代码并将其推送到Github。

也请检查https://levelup.gitconnected.com/utilizing-the-power-of-interfaces-when-mocking-and-testing-external-apis-in-golang-1178b0db5a32

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-