Comments (6)
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.
from go-mock.
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.
from go-mock.
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.
from go-mock.
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
from go-mock.
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.
from go-mock.
OK.Got it.Thanks
from go-mock.
Related Issues (8)
- Mock not working HOT 1
- Question [Not a issue] HOT 4
- Support variable number of arguments HOT 6
- Feature Request: Given an interface, generate a mock HOT 3
- When using MockFunction.Call, only the first set of return values is returned HOT 1
- what if the function return a struct, how can i mock it? HOT 1
- Please add Go module HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-mock.