I have put together a piece of code which does a GET on my route. I would like to test this using mocking. I am a Go and a test noob, so any tips are greatly appreciated.
My Generate Routes.go generates the routes for the current URL. Snippet:
func (h *StateRoute) GenerateRoutes (router *martini.Router) *martini.Router {
r := *router
/**
* Get all states
*
*/
r.Get("/state", func( enc app.Encoder,
db abstract.MongoDB,
reqContext abstract.RequestContext,
res http.ResponseWriter,
req *http.Request) (int, string) {
states := []models.State{}
searchQuery := bson.M{}
var q *mgo.Query = db.GetDB().C("states").Find(searchQuery)
query, currentPage, limit, total := abstract.Paginate(req, q)
query.All(&states)
str, err := enc.EncodeWithPagination(currentPage, limit, total, states)
return http.StatusOK, app.WrapResponse(str, err)
})
}
And this is being called in my server.go as such:
var configuration = app.LoadConfiguration(os.Getenv("MYENV"))
// Our Martini API Instance
var apiInstance *martini.Martini
func init() {
apiInstance = martini.New()
// Setup middleware
apiInstance.Use(martini.Recovery())
apiInstance.Use(martini.Logger())
// Add the request context middleware to support contexual data availability
reqContext := &app.LRSContext{ }
reqContext.SetConfiguration(configuration)
producer := app.ConfigProducer(reqContext)
reqContext.SetProducer(producer)
apiInstance.MapTo(reqContext, (*abstract.RequestContext)(nil))
// Hook in the OAuth2 Authorization object, to be processed before all requests
apiInstance.Use(app.VerifyAuthorization)
// Connect to the DB and Inject the DB connection into Martini
apiInstance.Use(app.MongoDBConnect(reqContext))
// Add the ResponseEncoder to allow JSON encoding of our responses
apiInstance.Use(app.ResponseEncoder)
// Add Route handlers
r := martini.NewRouter()
stateRouter := routes.StateRoute{}
stateRouter.GenerateRoutes(&r)
// Add the built router as the martini action
apiInstance.Action(r.Handle)
}
My doubts:
How does the mocking work here, considering I am trying to inject the dependency?
Where should I start the testing from i.e. should I mock up r.Get in the Generate Routes? Right now, I've done this but since I'm using Martini which handles all the routing and requests, I'm quote lost if what I've done is right?
state_test.go:
type mockedStateRoute struct {
// How can I mock the stateRoute struct?
mock.Mock
}
type mockedEncoder struct {
mock.Mock
}
type mockedMongoDB struct {
mock.Mock
}
type mockedReqContext struct{
mock.Mock
}
type mockedRespWriter struct{
mock.Mock
}
type mockedReq struct{
mock.Mock
}
func (m *mockedStateRoute) testGetStatesRoute(m1 mockedEncoder,
m2 mockedMongoDB, m3 mockedReqContext,
m4 mockedReqContext, m5 mockedRespWriter,
m6 mockedReq) (string) {
args := m.Called(m1,m2,m3,m4,m5,m6)
fmt.Print("You just called /states/GET")
// 1 is just a test value I want to return
return 1, args.Error(1)
}
func TestSomething (t *testing.T) {
testObj := new(mockedStateRoute)
testObj.On("testGetStatesRoute", 123).Return(true,nil)
// My target function that does something with mockedStateRoute
// How can I call the GET function in GenerateRoutes(). Or should I, since martini is handling all my requests
}
Links I've referred to: