Coder Social home page Coder Social logo

Comments (6)

maraino avatar maraino commented on June 30, 2024

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.

soldiershen avatar soldiershen commented on June 30, 2024

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.

maraino avatar maraino commented on June 30, 2024

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.

soldiershen avatar soldiershen commented on June 30, 2024

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.

maraino avatar maraino commented on June 30, 2024

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.

soldiershen avatar soldiershen commented on June 30, 2024

OK.Got it.Thanks

from go-mock.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.