Questions tagged [testify]

A sacred extension to the standard go testing package

Testify is a set of golang packages that make testing your Golang code easier.

  • Easy assertions
  • Mocking
  • HTTP response trapping
  • Testing suite interfaces and functions

More info can be found at github.com/stretchr/testify

104 questions
1
vote
1 answer

Mock function without receiver

I have the file util.go: func Foo(service *SomeService) error { return helper(service) } func helper(service *SomeService) error { ... } I'm writing unit tests using testify, starting with Foo. I want to: mock helper assert mocked helper…
onepiece
  • 3,279
  • 8
  • 44
  • 63
1
vote
0 answers

Test cases are not working in gin framework

I have written test case to test method in Gin-Gonic framework but it in not giving expected output while running test case. But when I run method using API in Postman it is working properly. Test case func TestList(t *testing.T) { router :=…
Amit Upa
  • 307
  • 1
  • 2
  • 13
1
vote
1 answer

How to test an HTTP function which takes folder as an input?

I have an HTTP handler function (POST) which allows a user to upload a folder from a web browser application. The folder is passed from JavaScript code as an array of files in a folder and on the backend (Go API) it is accepted as a…
1
vote
1 answer

Mock Interface function not getting called

I'm trying to write Go Unit Test using testify mocking library. I was following this blog http://goinbigdata.com/testing-go-code-with-testify/. I have passed the mocked interface to the newCalculator function but still Random1 of Random interface is…
1
vote
1 answer

How to define that mock method gets invoked zero times

I'm trying to test the following method: //AuthenticationMiddleware Middleware which handles all of the authentication. func AuthenticationMiddleware(context context.ContextIntf, w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) { …
Mike Warren
  • 3,796
  • 5
  • 47
  • 99
1
vote
1 answer

Difference between SetupSuite & SetupTest in Testify Suites

I am trying to figure out difference between SetupSuite and SetupTest for quite some time now. Based on information on blogs I have understood that SetupSuite is run before entire suite and SetupTest runs before each test case. But what could be…
Mohit Jain
  • 733
  • 3
  • 9
  • 24
1
vote
1 answer

Is there a way to chain asserts with testify?

I really like what testify brings to go test. However, I dug through the documentation and didn't see anything on how to handle multiple asserts. Does Go handle "first failure", in the sense it fails at the first bad assert, or will it only concern…
user6467981
1
vote
1 answer

How to test this simple method with Go?

I'm writing some unit tests, and I'm stuck writing a test for the following method: func (database *Database) FindUnusedKey() string { count := 0 possibleKey := helpers.RandomString(helpers.Config.KeySize) for…
Gregorio Di Stefano
  • 1,173
  • 1
  • 15
  • 23
0
votes
0 answers

How to pass an ellipsis argument to a function mocked with testify?

I'm using testify for my unit tests and I have a mock structure created by mockery. My interface looks like this: type Foo interface { Bar(...interface{}) } Mockery creates the following struct using testify: type Foo struct { mock.Mock } //…
akko
  • 559
  • 3
  • 8
  • 17
0
votes
1 answer

How do you mock calls on structs (e.g. csv.Writer) in Go?

Here is some code type Combiner interface { Combine(files []io.Reader) (io.Reader, error) } type CsvCombiner struct { hasHeader bool } func (c *csvCombiner) Combine(files []io.Reader) (io.Reader, error) { combinedCSV := &bytes.Buffer{} …
0
votes
0 answers

Mock go function that modifies and returns the value passed as argument

I have a function in Go that takes an input argument, modifies it, and finally returns it. To be concrete, this function persists some data in DB using the Bun ORM and returns the persisted entity. That said, I have a unit test where I mock this…
beni0888
  • 1,050
  • 1
  • 12
  • 40
0
votes
1 answer

Table-driven test always shows parent as failing when using testify

When using testify with table-driven tests as in: func TestFoo(t *testing.T) { a := assert.New(t) tc := []struct { desc string foo string }{ { desc: "fail abc", foo: "abc", }, …
0
votes
1 answer

All packages of golang testify not getting download

Am trying to download all testify packages of Golang. I am running go get github.com/stretchr/testify. After that if I run go mod vendor only assert package is getting download. I need other packages too, like suite, mock but for some reason not…
Exploring
  • 925
  • 6
  • 18
0
votes
1 answer

Go testify package: segfault when trying to avoid ambiguous call

I am writing the following unit test: type mockQux struct { mock.Mock MockInterface calculateHash func(ctx context.Context, foo *entities.Foo, bar model.Bar, baz *model.Baz) (uint64, error) } type MockInterface interface { …
olithad
  • 50
  • 4
0
votes
0 answers

Unittest for opts passed to mock

I have some code, for instance: type FileFilter func(*models.File) bool func ByExtension(ext string) FileFilter { return func(file *models.File) bool { // it's just an example do not blame here :) return true } } type…
Mike
  • 860
  • 14
  • 24