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

Multiple mocks matchers #30

Open
emadolsky opened this issue Dec 20, 2017 · 2 comments
Open

Multiple mocks matchers #30

emadolsky opened this issue Dec 20, 2017 · 2 comments

Comments

@emadolsky
Copy link

So I was writing some tests for my code.
In one of my functions I was calling two APIs (for different purposes). So I needed to have two mockers. one of them(route2) should have had a matcher for the request body while the other (route1) did not. (For this, I had make two mockers with gock.New)
Then I traced an unexpected behavior. When I was calling the API to route2, the matcher registered on route1 mocker was being called.
Therefore, I looked up in the code to see the reason and I found out that there was a global variable named "Matchers" in "matcher.go". And this causes all the mockers to have the same matchers which I believe is a bug.
My test code:

	//route1
	gock.New("http://emad.host").
		Get("/notifications/bucketname").
		Reply(200).
		JSON(map[string]interface{}{
		"ok": "ok",
	})
	//route2
	gock.New("http://emad.host").
		Put("/notifications/bucketname").
		AddMatcher(func(r *http.Request, request *gock.Request) (bool, error) {
		fmt.Println("matcher called")
		return true, nil
	}).Reply(400)

	client := &http.Client{}

	//this request would call matcher of route2 (unexpected behaviour)
	request1, _ := http.NewRequest("GET", "http://emad.host/notifications/bucketname", nil)
	fmt.Println(client.Do(request1))

	request2, _ := http.NewRequest("PUT", "http://emad.host/notifications/bucketname", bytes.NewReader([]byte(`hello world`)))
	fmt.Println(client.Do(request2))
@h2non
Copy link
Owner

h2non commented Dec 21, 2017

This is something that has been discussed previuosly here:
#26

You are very welcome to contribute. I'm open to include this capability.
I have implemented it in pook and I'm relatively happy with that design decision.

@danny-cheung
Copy link
Contributor

This is a variation of #20 and is fixed by pr #55

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

No branches or pull requests

3 participants