How to mock for same input and different return values in a for loop in golang

I had a similar problem. The solution was the method Once() In your mock add an .Once() and repeat the mock with each result you need. Something like this: lib.Mock.On(“method”, arg).Return(test.mockError).Once() lib.Mock.On(“method”, arg).Return(nil).Once() Each mock result will be returned only once. https://godoc.org/github.com/stretchr/testify/mock#Call.Once

Unit testing for functions that use gorilla/mux URL parameters

gorilla/mux provides the SetURLVars function for testing purposes, which you can use to inject your mock vars. func TestGetRequest(t *testing.T) { r, _ := http.NewRequest(“GET”, “/test/abcd”, nil) w := httptest.NewRecorder() //Hack to try to fake gorilla/mux vars vars := map[string]string{ “mystring”: “abcd”, } // CHANGE THIS LINE!!! r = mux.SetURLVars(r, vars) GetRequest(w, r) assert.Equal(t, http.StatusOK, … Read more

Separating unit tests and integration tests in Go

@Ainar-G suggests several great patterns to separate tests. This set of Go practices from SoundCloud recommends using build tags (described in the “Build Constraints” section of the build package) to select which tests to run: Write an integration_test.go, and give it a build tag of integration. Define (global) flags for things like service addresses and … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)