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
2
votes
1 answer

How to test Golang channels / go-routines

I have a type that contains a byte of data, and takes a channel to post new data there. Other code can read the last written byte of data using a Read function. Edit: for actual, runnable code, see https://github.com/ariejan/i6502/pull/3 especially…
Ariejan
  • 10,910
  • 6
  • 43
  • 40
1
vote
0 answers

Testing a service by mocking repository with testify/mock fails

I am new to Golang. I am trying to write unit tests for a service file. I am using testify and it's packages testify/mock and testify/assert. Looking at different articles indicates that the MockCommentRepository written below is correct. But, one…
IamGhale
  • 1,275
  • 2
  • 14
  • 26
1
vote
0 answers

Checking external private errors

I have a package which does some error checking for specific error types. One of the errors is from an external package, its fields are all unexported as well as the relevant creation func, so I cant instantiate it in my test, like below. type…
Person1
  • 109
  • 2
  • 10
1
vote
0 answers

mock db connection with testfy golang

Context I have a simple gin based Rest API implementing CRUD over a redis DB. I want to mock the redis client (github.com/go-redis/redis/v9) in my unit tests. Coming from OOP, I feel that I am transposing some patterns wrongly. Here is the route I…
zar3bski
  • 2,773
  • 7
  • 25
  • 58
1
vote
0 answers

Golang: optimal variant run tests in transaction

Creating tests for database models. My goal test all testCases in transactions. Uses "github.com/stretchr/testify/suite" Closure use to create records in transaction. What do you think about this approach? This code works, but I think: may be there…
greenif
  • 1,055
  • 1
  • 12
  • 25
1
vote
1 answer

Golang Testify Mock does not return specified values

I've mocked a repository interface and I want to return a specific value when a method is called. However, it always returns what's in the dummy implementation. type MockUserRepo struct { mock.Mock } // dummy method to fulfil the interface func…
Acha Bill
  • 1,255
  • 1
  • 7
  • 20
1
vote
1 answer

Potentially unused parameter in go when using testify's suite package

I want to execute subtests using the testify/suite package. I am declaring my Unit suite as follows type UnitSuite struct { suite.Suite } func TestUnitSuite(t *testing.T) { suite.Run(t, &UnitSuite{}) } and here is my subtest func (us…
pkaramol
  • 16,451
  • 43
  • 149
  • 324
1
vote
1 answer

Right way to assert that slice of pointers to strings contains expected strings?

Is there an easy and compact way using Testify to assert that a slice of pointers to strings contains a pointer to a string that matches my expectation? Imagine that you're getting a slice of pointers to strings back from a function call (maybe from…
Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52
1
vote
1 answer

Testing In Golang, how to test cache that expires in 30 seconds

I have an interface called localcache: package localcache type Cache interface { Set(k string, v interface{}) error Get(k string) (interface{}, error) } and another file containing its implementation type cache struct { pool…
fanfan
  • 39
  • 1
  • 5
1
vote
1 answer

Writing test for a Gin API using testify results in a 404 HTTP response code

I have attempted to write a test for my Gin API using testify. Unfortunately, it responds with an unexpected HTTP 404 response code within the test. When I execute the program I can reach the corresponding interface via curl and browser. Why are my…
ZPascal
  • 323
  • 2
  • 4
  • 14
1
vote
0 answers

Mocking a method that stores result in pointer argument

I want to set test reportdata from my unit test function as i m mocking GetTestFunc but in real GetTestFunc is returning only error and is setting my pointer variable TestReport. but while using mock we are returning but seeting of pointer variable…
1
vote
1 answer

Go goroutine test failing Expected number of calls

I'm new to Go here. I am trying to test the function call inside my Go routine but it fails with the error message Expected number of calls (8) does not match the actual number of calls (0). My test code goes like: package executor import ( …
nerdy_darkknight
  • 615
  • 1
  • 8
  • 16
1
vote
0 answers

How to unit-test/mock C binding implementation in Go?

I would like to implement a unit-test for this implementation: import "C" // Model represents a TensorFlow Lite Model. type Model struct { model *C.TfLiteModel } // NewModelFromFile creates a new TensorFlow Lite Model from File. func…
Nicolas Bortolotti
  • 555
  • 1
  • 8
  • 22
1
vote
1 answer

pg-go RunInTransaction not rolling back the transaction

I'm trying to rollback a transaction on my unit tests, between scenarios, to keep the database empty and do not make my tests dirty. So, I'm trying: for _, test := range tests { db := connect() _ = db.RunInTransaction(func() error { …
1
vote
1 answer

How to unit test a Cobra CLI command which requires internal mocks?

I have defined the following Cobra subcommand that I'd like to test the output for using testify: var childCmd = &cobra.Command{ Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { id := args[0] …
vercingortix
  • 199
  • 2
  • 15