Questions tagged [go-testing]
121 questions
0
votes
1 answer
HttpMock is not intercepting Resty call
I have a function that calls an external api that I want to mock out in the test.
func ApiWrapper(...) (...) {
client := resty.New()
var r apiResponse
apiPath := "..." // In the test will be http://localhost:{PORT}/path/to/endpoint
_, e :=…

Dan
- 1,874
- 1
- 16
- 21
0
votes
1 answer
Go test to validate connect2id gives "invalid_client" error
I'm trying to validate the Connect2id set up with Go test and I'm getting following error.
"Client authentication failed: Missing client authentication","error":"invalid_client"
The full scenario output is look like below.
Feature: Test Identity…

AnujAroshA
- 4,623
- 8
- 56
- 99
0
votes
1 answer
How to interrogate an error object inside a GoLang Test case?
I'm writing my first API endpoint in GoLang using GRPC/proto-buffers. I'm rather new to GoLang. Below is the API in action in the happy case:
$ grpcurl -d '{
"field1": "A",
}' -plaintext localhost:11000…

Saqib Ali
- 11,931
- 41
- 133
- 272
0
votes
1 answer
Why doesn't t.Fail() accept string arguments?
I am trying to improve my Golang tests. And I was reading this: https://ieftimov.com/post/testing-in-go-failing-tests/
I was using t.Fatal("message") a lot, when instead I should have been using a combination of:
t.Fail()
t.Logf()
so why on Earth…
user12211419
0
votes
1 answer
Middleware HTTP test passing when it shouldn't
I've written some middleware that checks to make sure a JWT token in valid:
func JwtVerify(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
//Get the token from the header
…

Tom Withers
- 1,171
- 4
- 16
- 28
0
votes
1 answer
How to mock many urls to return fixture content?
I'm writing some kind of a recursive parser. The simplest form is:
Take all links from first link's page body
Repeat the first step for each link
So now I want to test it. The problem is I can't figure out the best way to mock all these pages. I…

Stanislav
- 47
- 2
- 5
0
votes
1 answer
Why am I getting ‘panic: runtime error: invalid memory address or nil pointer dereference’ accessing Firestore on new computer?
I get a Panic if I attempt to run the tests or runthe app directly via: go run main.go
The code works great on my old Macbook Pro. It panics on my new Macbook Pro.
I believe the error has Something to do with accessing the Firestore datastore but…

GolangNewb
- 125
- 2
- 8
0
votes
1 answer
Send go test coverage output to stdout - send it to S3
I want to send the html for go test -c to stdout, so I can serve the resulting HTML from S3 static assets server.
Something like this:
arti_fact="s3://cm-html/cm-api/$commit_sha"
go test -coverprofile cover.out .
go tool cover -html=cover.out -o…
user12211419
0
votes
1 answer
Mocking an interface defined in a test file: interface "Undefined"?
This is a bug which I've attempted to reproduce with a minimal example, but so far unsuccessfully. The Go module is similar to the following:
.
├── go.mod
└── handler
├── handler.go
├── handler_test.go
└── mock_handler.go
where…

Kurt Peek
- 52,165
- 91
- 301
- 526
0
votes
2 answers
Test fails to capture logging output
I am trying to test my UserRegister functionality, it takes http request.
If user enters already existing email, UserRegister returns an error log (using logrus).
logs "github.com/sirupsen/logrus"
func UserRegister(res http.ResponseWriter, req…

Sachith Muhandiram
- 2,819
- 10
- 45
- 94
-1
votes
0 answers
Unit test case is not executing inside return func (go)
I'm trying to execute unit test for below function but it not completely executing all line.
func (a API) FetchingTextFile() gin.HandlerFunc {
return func(c *gin.Context) {
f := c.Param("fileType")
files, _, err :=…

ROHITH P
- 17
- 3
-1
votes
1 answer
hex-arc golang - overcome import cycling error when testing
I am having trouble testing foo_handler in my Go project. My project has the following structure:
├── Makefile
├── cmd
│ └── main.go
├── go.mod
├── go.sum
└── internal
├── api
│ ├── router.go
│ └── server.go
├── core
│ …

Dana
- 57
- 1
- 4
-1
votes
2 answers
How do I pass arguments to run the test code
I have two files main.go and main_test.go
under main.go
package main
import (
"fmt"
"os"
"strconv"
)
func Sum(a, b int) int {
return a + b
}
func main() {
a, _ := strconv.Atoi(os.Args[1])
b, _ := strconv.Atoi(os.Args[2])
…

Mesc
- 21
- 1
- 9
-1
votes
1 answer
Expectations unmatched : Golang unit test error
I am new to Golang,I am getting below error when I try to run below testcase. Can someone please suggest what am I doing wrong here?
db, mock, _ := sqlmock.New()
dbConnect, _ := services.DB.Connect(services.Config.Database)
defer…

Mitul
- 83
- 1
- 5
-1
votes
2 answers
How to mock go-plugin funcs in golang?
# I want to mock this function
func testCheckPluginFile(fName string){
plugin, _ := plugin.Open(path.Join("/I/expect/folder/","/plugin-lib-test/"+fName))
plugin.Lookup("symbol")
}
# So I put this func like this
func…

Harrien
- 9