Closed soldiershen closed 7 years ago
Hi @soldiershen, I don't understand your problem, can you provide a more complete example and some kind of proposal of how would you address your problem.
Hi @maraino,Thanks for your answer. I am doing the UT for a service project but get a code design issue. code logic Controller-Dao-DB Controller func:
func QuerySparkAggregateMeetingStatistics(context *gin.Context) {
...//query param in context
obj:=daoObj{...}
obj.QuerySthFromDB(...)
}
x interface{ QuerySthFromDB(...) } daoObj implements the x
I know if daoObj is passed by the func as a interface like func QuerySparkAggregateMeetingStatistics(x interface,context gin.Context){...} rather than created in the func body I could use go-mock to mock daoObj and its action(query sth). But as gin(a framework) requires,this controller func needs to put only one param(gin.Context) How could I use go-mock to mock when I try to do UT for this controller func.
Hi @soldiershen
One solution is to create a constructor for the daoObj, and use something in the context to return the mock or the real object.
type Dao interface{
QuerySthFromDB(...)
}
func NewDaoObj(ctx *gin.Context) Dao {
if v, ok := ctx.Get("mock.dao"); ok && v.(bool) {
return &MockDao{}
}
return &daoObj
}
func QuerySparkAggregateMeetingStatistics(ctx *gin.Context) {
obj := NewDaoObj(ctx)
obj.QuerySthFromDB(...)
}
And obviously set the mock.dao in the context in your unit tests.
yes I did it as same way before but it seems weird since context is the context.I could not pour sth weird into context to meet my requirement. Sounds like UT in golang has over reliance on func to pass param to control actions Do you have any idea to avoid it? I am using monkey to mock now to get rid of the reliance
I usually have my own context object with all the external services, when I'm doing unit tests I construct that context with the mocked objects, and when I'm running the service, I create the context with the real ones.
OK.Got it.Thanks
like below one I have to use obj as a param to mock(control the behavior of QueryResultWithParamObjByPage.But it is not a good way since as the code grows I have to put more useless "obj" to mock.
func (e SparkStatisticsDaoEngine)QueryAggregateMeetingStatistics(xxx,paramObj model.SparkRequestParamObject, session baseDao.Session,obj) ([]interface{}, error) { session.SetPageSize(paramObj.PageSize) resultMappingFunc := func(iter baseDao.Iterator) (interface{}, bool) { var item model.AggregateMeeting hasNext := iter.Scan(&item.OrgId, &item.AveragePeakNumberOfParticipants) return item, hasNext } param:=xxx return obj.QueryResultWithParamObjByPage(param) }