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

Function like assert.Contains by stretchr/testify but ignore case and whitespace

For example, I have this test here assert.Contains(t, "HELLO WORLD", "hello world) and I want this to return true. Obviously, I can clean up the string with strings.TrimSpace(), strings.ReplaceAll(), and strings.ToLower() beforehand. Although…
cxc
  • 201
  • 2
  • 10
0
votes
0 answers

Same file needed for multiple packages in GoLang

I am using this method here to collect the code coverage for my system tests. The overall idea is that I need a dummy test so that I can generate an executable that calls the main() in the package. As a result, I have a file like the following in…
cxc
  • 201
  • 2
  • 10
0
votes
1 answer

assert: mock: I don't know what to return (even if I've declared the mock function & the return)

I use Testify to create a unit test for my golang app. I need to create a unit test for this function where it calls a variadic function (function with trailing arguments). I encountered an error when I test it. I'm actually not sure if the error is…
new line
  • 183
  • 2
  • 9
0
votes
1 answer

Mock Method2 of Interface called in another method1

I am writing unit tests for Method1() I want to mock Method2() that is called from Method1() of the same interface. We can mock methods of another interface easily by taking that mocked implementation in the struct. But I'm not sure how to mock a…
yashjain12yj
  • 723
  • 7
  • 19
0
votes
1 answer

Golang data race condition in test case

So I have some pipeline. I have to compare 2 files, line by line and e.g load to database. I want to have some timeout for handling single line, and break the pipeline if it reached. Entering endpoint is function Run(). Here we do some validation…
0
votes
0 answers

Golang stretchr testify mock calls to a function within same class

I am pretty new to Golang and writing unit test for a function (I am using stretchr testify for mocks). This function however calls another function in same class. But when unit testing the first function, I want to mock the call to second function…
0
votes
1 answer

Trying to mock a http.Client and getting error

Hey so I have seen and used this post to help mock my http.Client but when I try to pass the mock request I get the following error: cannot use mockClient (variable of type *MockClient) as *"net/http".Client value in argument to api.callAPI. In one…
maura
  • 17
  • 3
0
votes
1 answer

testify using real function result as mocked function argument

How can I test this function I need to make sure mocked FindByID function return value is a user with created UUID. func (s *Service) Register(newUser domain.User) (domain.User, error) { newUser.ID = uuid.New() s.repo.Create(newUser) …
BigLOL
  • 241
  • 3
  • 9
0
votes
1 answer

Azure SDK for Go - asserting on *keyVault.Properties.EnableSoftDelete nil or false

I have an issue that I have now spent two hours on. I have a Go test that deploys a Key Vault through the Azure SDK for Go. note that I have this in my imports: "github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2016-10-01/keyvault" Here is…
RuSs
  • 1,725
  • 1
  • 29
  • 47
0
votes
3 answers

How do I not use an interface when mocking?

I am trying to avoid using an interface because my use cases do not require it (similarly mentioned in https://www.ardanlabs.com/blog/2016/10/avoid-interface-pollution.html). However, to create a mock (using testify), wouldn't I need an interface to…
imagineerThat
  • 5,293
  • 7
  • 42
  • 78
0
votes
1 answer

GoLang Sarama ConsumerGroup Mocking

I am new to Go and I am struggling to mock out the call too: sarama.NewConsumerGroup(brokers, group, config) I am using testify and my mocked code currently looks like: type MyMockedObjectReciever struct { mock.Mock Receiver } func (m…
Silk13
  • 47
  • 2
  • 9
0
votes
1 answer

How to pass json data as request parameter while mocking using gin framework

I have a function to create user which is working properly. Now I have to mock Prepare and SaveUser function inside CreateUser. But that CreateUser require json data as request parameter. Below is my CreateUser function. func (server *Server)…
Amit Upa
  • 307
  • 1
  • 2
  • 13
0
votes
1 answer

Returning a Mock from a package function

I'm fairly new to Go and I'm having some issues with writing tests, specifically mocking the response of a package function. I'm writing an wrapper lib for github.com/go-redis/redis. At the moment it only really has better errors for failures, but…
SynackSA
  • 855
  • 1
  • 12
  • 35
0
votes
1 answer

Testify mock is returning assertion that the function has not been called

My tests keep failing with but no actual calls happened but I am positive the func is getting called (It's a logging function so I see the logs on the terminal) Basically I have code that looks something like this : common/utils.go func…
ExceptionHandler
  • 213
  • 1
  • 8
  • 24
0
votes
1 answer

Golang unit testing using PanicsWithValue

We are trying to test a function that raises an index out of range error. The code of the unit test is simple, something like: import ( "testing" "github.com/stretchr/testify/assert" ) func TestIndexOutOfRange(t *testing.T) { …
Helder Sepulveda
  • 15,500
  • 4
  • 29
  • 56