MakeAWishFoundation / SwiftyMocky

Framework for automatic mock generation. Adds a set of handy methods, simplifying testing. One of the best and most complete solutions, including generics support and much more.
MIT License
1.03k stars 104 forks source link

Stubber with factory behavior #341

Open jaanus opened 1 year ago

jaanus commented 1 year ago

I was expecting this to work:

someMock.given(.aFunction(willProduce: { stubber in
  stubber.return(resultFactory())
}))

It does work, but not as I expect. I was expecting the stubber block to be called every time the method on the mock is invoked, and return a fresh result from the factory.

What actually happens: stubber block is ran only once, correctly (once) runs the factory block, captures its result, and keeps wrapping over it and returning the same result to all method invocations.

I realize why working with static results is OK most of the time. I’m not proposing to change anything about current behavior, but rather, to add a capability to stubber where it would let me return fresh results from a factory. I suppose it would be a new stubbing policy in addition to wrap and drop.