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 do I create an unit test expectation for a method that I imported from external package?

I have this function that I wanna test. I use testify to create the unit test. // calculate.go import ( "context" "errors" "testing" "github.com/jmoiron/sqlx" ) func (u usecase) CalculateTransaction(ctx context.Context, ID int64) (err…
new line
  • 183
  • 2
  • 9
2
votes
1 answer

assert: mock: I don't know what to return because the method call was unexpected

I'm trying to create a unit test on Go using testify. The function I'm trying to test is quite simple, it'll start an SQL transaction, and then get values from DB through the repo layer, and then calculate those values. But whenever I'm trying to…
new line
  • 183
  • 2
  • 9
2
votes
2 answers

Testify, mock unexpected method call when the expectation is already written , but the method is called twice with different parameter

I have an use case method that call a mocked repository method two times with a different parameter I've written it somewhat like this func TestInitUserSubscription(t *testing.T) { aRepository := &aRepositoryMock{} //this expected method is…
ggk
  • 117
  • 2
  • 9
2
votes
1 answer

Go assert.Equal some interface with time.Time object inside

I am trying to check if the returned data equals the expectation Here is my function: func extractData(payload string) (interface{}, time.Time, error) { eventTime := gjson.Get(payload, "data.eventDateTime").String() dateTime, err :=…
Jasiuoo
  • 41
  • 1
  • 1
  • 2
2
votes
1 answer

What's the most idiomatic way of testing functions with HTTP requests in Go?

I have a fun little weather app. For only $99/day, the app will check the weather daily, and if it's raining in Seattle, send an umbrella to the people of San Diego. I use these two functions as part of my app: func IsRaining() (bool, error) { …
2
votes
0 answers

Golang mocking API call using resty library

I'm writing some test to check some responses on one of my APIs but the resty mock client is not being sent to the original function. I created a mock client for resty using the readme example but the info on mocking resty is very incomplete. The…
parebi
  • 29
  • 2
2
votes
4 answers

Verify order of mocks call

I use testify (v1.6.1) and need to test if methods of interfaces call in right order. I checked the documentation and tried to find any information in the internet, but didn't find anything about mocks order checking. Example: type InterfaceA…
kozmo
  • 4,024
  • 3
  • 30
  • 48
2
votes
1 answer

Mock interface method twice with different input and output using testify

How can I mock an interface method twice in golang test? For example: type myCache interface{ Get(key string, data interface{}) error } type service struct { cache myCache } func (s service) GetBookDetail() (BookDetail, error) { ... …
dev-x
  • 897
  • 3
  • 15
  • 30
2
votes
1 answer

How to abort testing if critical tests fail

In Go, is there a way to abort a suite of tests early if one of them fails? I am using stretchr/testify suites but this just builds on the basic go testing functionality. Some options I have considered: I looked at setting testing.failFast but it…
Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
2
votes
4 answers

How to receive multiple values returned by a method in testify framework "assert" method as an argument?

Below is a sample code , which is returning multiple values . func (c Calc) CreateTenantHandler(item *models.TenantInput) (*models.Response, *models.ErrorDetails) { ... ... ... return…
infiniteLearner
  • 3,555
  • 2
  • 23
  • 32
2
votes
1 answer

How to mock only one method of an interface

I am struggling to understand mocking in Go (am looking for something related to Mockito.spy equivalent of java in Go). Let's say I have an interface in Go with 5 methods. But the piece of code that I want to test has references to only two methods.…
Mozhi
  • 757
  • 1
  • 11
  • 28
2
votes
0 answers

Mocking a struct with dependencies in Golang

I saw that on here, you can have a framework called Mockery generate mocks for interfaces for you. This is close to a big deal, because I am already using testify, which has built-in mock API (which, until now, I have not used yet). The drawback,…
Mike Warren
  • 3,796
  • 5
  • 47
  • 99
2
votes
1 answer

Initialization loop Golang

Hi I'm trying to make my function unit testable. One of the suggestion was to assign the function into a variable and make it globally accessible. I just did that but now I'm experiencing a Initialization loop below is my…
MadzQuestioning
  • 3,341
  • 8
  • 45
  • 76
2
votes
2 answers

Is it possible to dynamically assert if two values are equal or not equal when unit testing in Go?

I've just started using Go. I'm writing unit tests and I'd like to be able to test using a table, where the result to be compared to the actual result sometimes should or should not be equal. For example, this is the code that I currently have:…
3cheesewheel
  • 9,133
  • 9
  • 39
  • 59
2
votes
4 answers

Mocking method from golang package

I have been unable to find a solution to mocking methods from golang packages. For example, my project has code that attempts to recover when Os.Getwd() returns an error. The easiest way I can thinking of making a unit test for this, is by mocking…
Gregorio Di Stefano
  • 1,173
  • 1
  • 15
  • 23