All Questions
19
questions
2
votes
0
answers
131
views
How to do a unit-test in Handler layer in go?(Using gorm,,gin)
Recently I started writing unit tests on go.I received a task to conduct tests on all layers.I did it successfully on the repository and service layer, but there were problems on the handlers.I corny ...
1
vote
0
answers
2k
views
Handling sync.Once in golang testcases
I'm getting started with go-gin project. Some properties are initialized in a sync.Once.Do() method like below.
var doItOnce sync.Once
var structObject SomeStruct
func getProperty() SomeStruct {
...
0
votes
1
answer
928
views
How to test a handler in Gin which depends on external file?
I have a simple Gin server with one of the routes called /metadata.
What the handler does is it reads a file from the system, say /etc/myapp/metadata.json and returns the JSON in the response.
But ...
0
votes
1
answer
217
views
How to use main function inside test function when unit testing Gin
I want to unit test gin controller function.
package controllers
import (
"fmt"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
&...
4
votes
0
answers
3k
views
Testify mock ExpectedCall does not match expected call in gin handler, panics
I'm using gin-gonic for a server, and testify for testing and mocks, along with "testing" and "net/http/httptest"
The part of the interface that mocks the method:
func (m *...
4
votes
1
answer
1k
views
DB mock using sqlmock not working for testing Gin/GORM API?
I've an API written using Gin that uses GORM for ORM. The API works perfectly fine when using a real DB and accessing the API URL from the web browser. But I can't get a mocked unit test to pass:
func ...
0
votes
0
answers
689
views
How to unit test a function with gin context while initializing all dependencies through docker?
I am running my main function ,intializing database ,routers through docker then performing unit test for this particular api (masterController) which is a long code so not pasting it here.The problem ...
2
votes
1
answer
2k
views
How to mock a third-party struct with many methods and perform unit tests on endpoints that depend on the third-party struct?
I'm working with getstream's Go library in gin gonic and realized that my endpoints will be heavily dependent on stream_chat.Client.
For instance, in the following endpoint (/v1/chat/test-token), a ...
1
vote
1
answer
1k
views
How to test handler which uses middleware
How to test handler which uses middleware
I'm trying to make unit test for handler which uses middleware but not as a dependency.
My code for handler looks like this:
package handlers
import (
&...
1
vote
0
answers
326
views
golang gin middleware unit testing [duplicate]
I have a middleware handler function and I would like to be able to write unit tests for this function. How might I go about doing this in GoLang? I would like to be able to verify when I call to add ...
13
votes
1
answer
15k
views
How to unit test a Go Gin handler function?
I have a controller function like this....
func GetMaterialByFilter(c *gin.Context) {
queryParam := weldprogs.QueryParam{}
c.BindQuery(&queryParam)
materialByFilter, getErr := ...
0
votes
1
answer
673
views
Go Gin Gonic Unit Tests in Sub Folder
I have a Go API built using the Gin framework.
Reading the docs in the testing section here, i tried to implement something similar:
main.go
package main
import (
"mes/routes"
&...
0
votes
1
answer
3k
views
How to send POST request multipart/form-data field as array for unit testing?
I'm trying to make unit testing for my Go application that handles form data in multipart/form-data content type using gin (PostForm and PostFormArray) like
x, y := c.PostForm("x"), c....
3
votes
1
answer
4k
views
How to set a host on gin's test context?
I would like to write a unit test for a controller but I keep getting a runtime error. I found out that it is due to the absence of the Host on the request, the ClientIP() method and request body. How ...
1
vote
1
answer
2k
views
How to unit test extraction from cookie in Gin Gonic
I'm using gin gonic. I've a function that extracts a token from a cookie, which actually works. I'm using this function in a route handler and want to test the handler function, but I don't know how.
...