Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe function set with ObserveMocks() overwritten in api tests with reports #51

Closed
enrico5b1b4 opened this issue Jun 17, 2019 · 1 comment

Comments

@enrico5b1b4
Copy link
Collaborator

When setting an observe function with ObserveMocks() and running an apitest with a reporter, the observe function is not called.
Failing test scenario detailed below

func TestMocks_ApiTest_SupportsObservingMocksWithReport(t *testing.T) {
	var observedMocks []*mockInteraction
	reporter := &RecorderCaptor{}
	observeMocksCalled := false

	getUser := NewMock().
		Get("http://localhost:8080").
		RespondWith().
		Status(http.StatusOK).
		Body("1").
		Times(2).
		End()

	getPreferences := NewMock().
		Get("http://localhost:8080").
		RespondWith().
		Status(http.StatusOK).
		Body("2").
		End()

	New().
		Report(reporter).
		ObserveMocks(func(res *http.Response, req *http.Request, a *APITest) {
			observeMocksCalled = true
			if res == nil || req == nil || a == nil {
				t.Fatal("expected request and response to be defined")
			}
			observedMocks = append(observedMocks, &mockInteraction{response: res, request: req})
		}).
		Mocks(getUser, getPreferences).
		Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			bytes1 := getUserData()
			bytes2 := getUserData()
			bytes3 := getUserData()

			w.Write(bytes1)
			w.Write(bytes2)
			w.Write(bytes3)
			w.WriteHeader(http.StatusOK)
		})).
		Get("/").
		Expect(t).
		Status(http.StatusOK).
		Body(`112`).
		End()

	assert.Equal(t, 3, len(observedMocks))
	assert.True(t, observeMocksCalled)
}
@steinfletcher
Copy link
Owner

Thanks for the bug report @enrico5b1b4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants