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

feature: stack-like fake.functionReturns #139

Open
andreasscherman opened this issue Jul 20, 2019 · 1 comment
Open

feature: stack-like fake.functionReturns #139

andreasscherman opened this issue Jul 20, 2019 · 1 comment

Comments

@andreasscherman
Copy link

andreasscherman commented Jul 20, 2019

Hi!

As far as I can tell, we have two ways of issuing return values to a generated fake struct in counterfeiter. The first is to simply record a value which it returns, and the second one is to enumerate on which nth call a certain value is returned.

A feature which I would find useful would be to have a "stack" approach as well, where you would be able to rather than enumerate the return values, just put them on a stack-like object.

That is, rather than:

fake.functionReturnsOnCall(0, 5)
fake.functionReturnsOnCall(1, 6)

You'd be able to simply:

fake.functionReturns(5)
fake.functionReturns(6)

My use-case for this is currently to have a fake http client, and I have separate functions which you'd I'd to push a certain return value on it.

func (s object) setupAuthenticate() {
  s.httpClient.DoReturns("...")
}

func (s object) setupGetPost() {
  s.httpClient.DoReturns("...")
}

func HelloWorldTest(t) {
  setupAuthenticate()
  setupGetPost()
  // this function calls both an "authentication" and a "getPost" endpoint
  call('/post/5') 
}

I can of course enumerate the return calls inside the functions, but it'd lead to a lot higher coupling between the setup functions. I can also make a wrapper for this, but I think it's not an unreasonable thing to have inside the counterfeiter fake itself either!

@rittneje
Copy link
Contributor

rittneje commented Apr 4, 2021

I know this issue is from almost two years ago, but I just wanted to let you know there is actually a third way of specifying return values. You can set the DoStub field to a function that will be called when the Do method is invoked. So you could just make a stack object yourself to keep track of things, and then have DoStub just pop off that stack.

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