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

How to specify returning a value given a specific argument? #155

Open
chintanparikh opened this issue Feb 4, 2020 · 1 comment
Open

Comments

@chintanparikh
Copy link

Hi -

I have an interface with a method that is called multiple times where I can't guarantee the order it's called in. However, the arguments on each call are different. Is there any way for me to specify a return value for specific arguments? E.g. something like:

interface Adder {
	Add(a int, b int) c int
}

fakeAdder.ReturnsWithArguments(1, 1, 3)

This would return 3 whenever someone calls adder.Add(1, 1)

@hoegaarden
Copy link
Contributor

hoegaarden commented Jul 23, 2020

This is nothing that counterfeiter comes with OOTB, however you can implement it relatively easy yourself by providing a stub:

func TestSomething(t *testing.T) {
	fakeAdder := foooofakes.FakeAdder{}

	fakeAdder.AddCalls(func(a, b int) int {
		specialCases := map[int]map[int]int{
			1: {0: 20, 1: 30, 2: 40},
			2: {1: 45, 2: 55},
		}

		if x, ok := specialCases[a]; ok {
			if y, ok := x[b]; ok {
				return y
			}
		}

		return -1
	})

	for a := 0; a < 3; a++ {
		for b := 0; b < 4; b++ {
			t.Logf("Add(%d, %d) = %d", a, b, fakeAdder.Add(a, b))
		}
	}
}

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