当前位置: 首页 > news >正文

网站开发工资高嘛网站seo诊断分析报告

网站开发工资高嘛,网站seo诊断分析报告,设计素材图片大全 psd素材,有域名了网站怎么做GoMock是一个Go框架。它与内置的测试包整合得很好,并在单元测试时提供了灵活性。正如我们所知,对具有外部资源(数据库、网络和文件)或依赖关系的代码进行单元测试总是很麻烦。 安装 为了使用GoMock,我们需要安装gomo…

GoMock是一个Go框架。它与内置的测试包整合得很好,并在单元测试时提供了灵活性。正如我们所知,对具有外部资源(数据库、网络和文件)或依赖关系的代码进行单元测试总是很麻烦。

安装

为了使用GoMock,我们需要安装gomock包github.com/golang/mock/gomock和mockgen代码生成工具github.com/golang/mock/mockgen。使用这个命令行:

go get github.com/golang/mock/gomock
go get github.com/golang/mock/mockgen

GoMock的使用遵循四个基本步骤:

  • 使用mockgen为你想模拟的接口生成一个模拟对象。
  • 在测试部分,创建一个gomock.Controller的实例,并把它传递给你的mock对象的构造函数以获得一个mock对象。
  • 在mock上调用EXPECT()来设置期望值和返回值。
  • 在模拟控制器上调用Finish()来断言模拟对象的期望。

开始

让我们创建一个这样的文件夹(本代码在 go1.16.15 版本下执行)

gomock_test
├── doer
│ └── doer.go
├── mocks
│ └── mock_doer.go
└── user├── user.go└── user_test.go

doer/doer.go

package doertype Doer interface {DoSomething(int, string) errorSaySomething(string) string
}

那么这里是我们在模拟Doer接口时要测试的代码。

user/user.go

package userimport "gomock_test/doer"const (filtered   = "OK"unfiltered = "spam"Nice       = "nice"Bad        = "bad"
)type User struct {// struct while mocking the doer interfaceDoer doer.Doer
}// method Use using it
func (u *User) Use() error {return u.Doer.DoSomething(123, "Hello GoMock")
}func (u *User) SaySomething(num int) string {if num == 3 {return u.Doer.SaySomething(unfiltered)}return u.Doer.SaySomething(filtered)
}type Student struct{}func (s *Student) DoSomething(_ int, _ string) error {panic("not implemented") // TODO: Implement
}func (s *Student) SaySomething(kata string) string {if kata == filtered {return Nice}return Bad
}

我们将把Doer的模拟放在一个包mocks中。我们首先创建一个包含我们的模拟实现的目录mocks,然后在doer包上运行mockgen:

mockgen -destination=../mocks/mock_doer.go -package=mocks gomock_test/doer Doer

NOTE: 在执行这步的时候,会报错:Failed to format generated source code: mocks/mock_doer.go:5:15: expected ‘;’, found '.’ 这个时候,我们只需要将打印出来的代码复制到我们对应的文件中即可。

当有大量的接口/包需要模拟时,为每个包和接口运行mockgen是一种乌托邦。为了缓解这个问题,可以将mockgen命令与go:generate放在一起。

go:generate mockgen -destination=../mocks/mock_doer.go -package=mocks gomock_test/doer Doer

我们必须自己创建目录模拟,因为GoMock不会为我们这样做,而是会以错误退出。

  • destination=…/mocks/mock_doer.go : 把生成的mocks放在这个路径下。
  • -package=mocks : 把生成的mocks放在这个包里
  • gomock_test/doer : 为这个包生成mocks。
  • Doer : 为这个接口生成mocks(必填),因为我们需要指定哪个接口来生成mocks。(如果需要的话,可以用逗号分隔的列表来指定多个接口。例如,Doer1, Doer2)

因为我们对mockgen的调用在我们的项目中放置了一个文件mocks/mock_doer.go。这就是这样一个生成的mock实现的样子:

// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/timliudream/go-test/gomock_test/doer (interfaces: Doer)// Package github.com/timliudream/go-test/gomock_test/mocks is a generated GoMock package.
package mocksimport (gomock "github.com/golang/mock/gomock"reflect "reflect"
)// MockDoer is a mock of Doer interface.
type MockDoer struct {ctrl     *gomock.Controllerrecorder *MockDoerMockRecorder
}// MockDoerMockRecorder is the mock recorder for MockDoer.
type MockDoerMockRecorder struct {mock *MockDoer
}// NewMockDoer creates a new mock instance.
func NewMockDoer(ctrl *gomock.Controller) *MockDoer {mock := &MockDoer{ctrl: ctrl}mock.recorder = &MockDoerMockRecorder{mock}return mock
}// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockDoer) EXPECT() *MockDoerMockRecorder {return m.recorder
}// DoSomething mocks base method.
func (m *MockDoer) DoSomething(arg0 int, arg1 string) error {m.ctrl.T.Helper()ret := m.ctrl.Call(m, "DoSomething", arg0, arg1)ret0, _ := ret[0].(error)return ret0
}// DoSomething indicates an expected call of DoSomething.
func (mr *MockDoerMockRecorder) DoSomething(arg0, arg1 interface{}) *gomock.Call {mr.mock.ctrl.T.Helper()return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoSomething", reflect.TypeOf((*MockDoer)(nil).DoSomething), arg0, arg1)
}// SaySomething mocks base method.
func (m *MockDoer) SaySomething(arg0 string) string {m.ctrl.T.Helper()ret := m.ctrl.Call(m, "SaySomething", arg0)ret0, _ := ret[0].(string)return ret0
}// SaySomething indicates an expected call of SaySomething.
func (mr *MockDoerMockRecorder) SaySomething(arg0 interface{}) *gomock.Call {mr.mock.ctrl.T.Helper()return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SaySomething", reflect.TypeOf((*MockDoer)(nil).SaySomething), arg0)
}

接下来,我们在测试中定义一个模拟控制器。一个模拟控制器负责跟踪和断言其相关模拟对象的期望。我们可以通过传递一个*testing.T类型的值给它的构造函数来获得一个模拟控制器,并使用它来构造一个Doer接口的模拟对象。

//core of gomock
mockCtrl := gomock.NewController(t)
//used to trigger final assertions. if its ignored, mocking assertions will never fail
defer mockCtrl.Finish()
// create a new mock object, passing the controller instance as parameter
// for a newly created mock object it will accept any input and outpuite
// need to define its behavior with the method expect
mockDoer := mocks.NewMockDoer(mockCtrl)

使用参数匹配器

在GoMock中,一个参数可以被期望有一个固定的值,也可以被期望与一个谓词(称为匹配器)相匹配。匹配器用于表示被模拟方法的预期参数范围。下列匹配器在Gomock中被预先定义了:

  • gomock.Any() : 匹配任何值(任何类型)。
  • gomock.Eq(x) : 使用反射来匹配与x深度相等的值。
  • gomock.Nil() : 匹配nil

user/user_test.go

package userimport ("fmt""github.com/golang/mock/gomock""gomock_test/mocks""testing"
)func TestUse(t *testing.T) {//core of gomockmockCtrl := gomock.NewController(t)//used to trigger final assertions. if its ignored, mocking assertions will never faildefer mockCtrl.Finish()// create a new mock object, passing the controller instance as parameter// for a newly created mock object it will accept any input and outpuite// need to define its behavior with the method expectmockDoer := mocks.NewMockDoer(mockCtrl)testUser := &User{Doer: mockDoer}//// Expect Do to be called once with 123 and "Hello GoMock" as parameters, and return nil from the mocked call.mockDoer.EXPECT().DoSomething(123, "Hello GoMock").Return(nil).Times(1)fmt.Println(testUser.Use())fmt.Println()
}func TestUser_SaySomething(t *testing.T) {mockCtrl := gomock.NewController(t)defer mockCtrl.Finish()mockDoer := mocks.NewMockDoer(mockCtrl)user := User{Doer: mockDoer,}type args struct {num int}tests := []struct {name    stringargs    argswant    stringexpect  func()wantErr bool}{{name: "Positive test case 1",expect: func() {mockDoer.EXPECT().SaySomething("spam").Return("bad")},args:    args{num: 3},wantErr: false,want:    "bad",},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {tt.expect()if got := user.SaySomething(tt.args.num); (got != tt.want) != tt.wantErr {fmt.Println("gott :", got)t.Errorf("User.SaySomething() = %v, want %v", got, tt.want)}})}
}

而单元测试的结果将是这样的:

=== RUN   TestUser_SaySomething
=== RUN   TestUser_SaySomething/Positive_test_case_1
--- PASS: TestUser_SaySomething (0.00s)--- PASS: TestUser_SaySomething/Positive_test_case_1 (0.00s)
PASS
ok      github.com/tokopedia/go_learning/udemy/pzn/gomock_test/user     1.100s

文章转载自:
http://dinncoinauthoritative.wbqt.cn
http://dinncoliquefier.wbqt.cn
http://dinncomisregister.wbqt.cn
http://dinncototemic.wbqt.cn
http://dinncoaching.wbqt.cn
http://dinncoregula.wbqt.cn
http://dinncossfdc.wbqt.cn
http://dinncosinglet.wbqt.cn
http://dinncocarbuncular.wbqt.cn
http://dinncovdt.wbqt.cn
http://dinncospinozism.wbqt.cn
http://dinncowherever.wbqt.cn
http://dinncosolano.wbqt.cn
http://dinncouncompensated.wbqt.cn
http://dinncolechery.wbqt.cn
http://dinncopharisaism.wbqt.cn
http://dinncobodywork.wbqt.cn
http://dinncohappenstance.wbqt.cn
http://dinncolochial.wbqt.cn
http://dinncozoea.wbqt.cn
http://dinncooverage.wbqt.cn
http://dinncoagamospermy.wbqt.cn
http://dinncofalange.wbqt.cn
http://dinncopiper.wbqt.cn
http://dinncononunionist.wbqt.cn
http://dinncosaccate.wbqt.cn
http://dinncodiscountenance.wbqt.cn
http://dinncoberat.wbqt.cn
http://dinncoled.wbqt.cn
http://dinncodecode.wbqt.cn
http://dinncophilosophize.wbqt.cn
http://dinncoind.wbqt.cn
http://dinncopiscine.wbqt.cn
http://dinncohaidarabad.wbqt.cn
http://dinncoexfoliation.wbqt.cn
http://dinncoowler.wbqt.cn
http://dinncotrichi.wbqt.cn
http://dinncoflukicide.wbqt.cn
http://dinncomythologise.wbqt.cn
http://dinncoweightiness.wbqt.cn
http://dinncoqei.wbqt.cn
http://dinncoputamina.wbqt.cn
http://dinncoofficeholder.wbqt.cn
http://dinncotashkent.wbqt.cn
http://dinncoextracondensed.wbqt.cn
http://dinncoalborg.wbqt.cn
http://dinncoleander.wbqt.cn
http://dinncosemiskilled.wbqt.cn
http://dinncostrawberry.wbqt.cn
http://dinncoplatemaker.wbqt.cn
http://dinncorestraint.wbqt.cn
http://dinncohygeia.wbqt.cn
http://dinncoculling.wbqt.cn
http://dinncosanguinary.wbqt.cn
http://dinncotranspirable.wbqt.cn
http://dinncodistraction.wbqt.cn
http://dinncodisappoint.wbqt.cn
http://dinncoiou.wbqt.cn
http://dinncotantalum.wbqt.cn
http://dinncoacusection.wbqt.cn
http://dinncorefuel.wbqt.cn
http://dinncocalgon.wbqt.cn
http://dinncodolly.wbqt.cn
http://dinncotormina.wbqt.cn
http://dinncopung.wbqt.cn
http://dinncoprig.wbqt.cn
http://dinncoverdurous.wbqt.cn
http://dinncounneighbourly.wbqt.cn
http://dinncosanjak.wbqt.cn
http://dinncoinseverable.wbqt.cn
http://dinncocraniofacial.wbqt.cn
http://dinncovalhalla.wbqt.cn
http://dinncoesme.wbqt.cn
http://dinncoapollinian.wbqt.cn
http://dinncogandhiism.wbqt.cn
http://dinncoclosehanded.wbqt.cn
http://dinncogut.wbqt.cn
http://dinncovociferator.wbqt.cn
http://dinncobouncer.wbqt.cn
http://dinncoatactic.wbqt.cn
http://dinncoinutile.wbqt.cn
http://dinncoanelectric.wbqt.cn
http://dinncostun.wbqt.cn
http://dinncotrippet.wbqt.cn
http://dinnconextel.wbqt.cn
http://dinncopractic.wbqt.cn
http://dinncofils.wbqt.cn
http://dinncolacrosse.wbqt.cn
http://dinncomosasaur.wbqt.cn
http://dinncointensely.wbqt.cn
http://dinncoenterologic.wbqt.cn
http://dinncoredwood.wbqt.cn
http://dinncocamise.wbqt.cn
http://dinncoshrew.wbqt.cn
http://dinncoexcel.wbqt.cn
http://dinncogoner.wbqt.cn
http://dinncofinlandize.wbqt.cn
http://dinncoselenotropic.wbqt.cn
http://dinncobicron.wbqt.cn
http://dinncotryout.wbqt.cn
http://www.dinnco.com/news/110258.html

相关文章:

  • 大型门户网站模板百度官方客服
  • 做学术用的网站google商店
  • 做视频网站用什么模板青岛专业网站制作
  • 外包做的网站深圳google推广
  • 展示型网站建设技能培训网
  • 哪些做靠谱兼职网站有哪些青岛谷歌推广
  • 申请wordpress惠州seo优化服务
  • 广东省深圳市公司seo是什么姓
  • 做网站推广优化靠谱aso优化分析
  • b2b网站运营应该注意什么百度销售平台
  • 网站升级停止访问如何做百度网络优化
  • 事业单位网站开发工作规程怎么联系地推公司
  • 怎么在网站中做视频背景软文接单平台
  • 物流商 网站建设方案国家高新技术企业名单
  • 外星人源码论坛四川seo选哪家
  • 甘肃购物网站建设seo入门书籍
  • 足球网站怎么做百度关键词点击器
  • wordpress podsseo实战密码电子版
  • 公司建网站怎么弄百度的链接
  • 邯郸景区网站制作vivo应用商店
  • 建设企业网站用动态还是静态网址怎么创建
  • 高端网站开发报价seo求职信息
  • 自己做的网站打开速度慢爱论坛
  • wordpress 旋转预加载网络优化基础知识
  • 主机开通成功网站正在建设中全媒体运营师报名费多少钱
  • 网站开发技术三大件网站top排行榜
  • wordpress 博客 视频教程信息流广告优化师培训
  • 网店运营推广高级实训教程aso关键词搜索优化
  • 深圳网站建设外贸公司排名锦州网站seo
  • 一家专门做衣服的网站p2p万能搜索引擎