Comments (6)
Hey, @drauch. I'm not 100% sure there's wide enough applicability to roll something into the product, but while in a boring meeting, I whipped up a couple of alternatives to your particular implementation. You may enjoy them and they may ease your pain if nothing goes into FIE or if there's a significant delay:
using FakeItEasy.Configuration;
using FakeItEasy.Core;
namespace FIEQuestions;
public class Test
{
public interface IDoSomething
{
int Foo(int x);
}
private int ProduceValue1(IFakeObjectCall call) => 2 * call.GetArgument<int>(0);
private int ProduceValue2(IFakeObjectCall call) => call.GetArgument<int>(0) * call.GetArgument<int>(0);
[Fact]
public void TestWithExplicitQueue()
{
var fake = A.Fake<IDoSomething>();
var valueProducers = new Queue<Func<IFakeObjectCall, int>>(new[] { ProduceValue1, ProduceValue2 });
A.CallTo(() => fake.Foo(A<int>._)).ReturnsLazily(call => valueProducers.Dequeue()(call));
var result1 = fake.Foo(6);
var result2 = fake.Foo(-17);
result1.Should().Be(12);
result2.Should().Be(289);
// if we picked the 3rd one, we'd walk off the end of the queue; not great
// but I'm not sure what your switch does either
}
[Fact]
public void TestWithCustomRule()
{
var fake = A.Fake<IDoSomething>();
Fake.GetFakeManager(fake).AddRuleFirst(new MyRule(ProduceValue1, ProduceValue2));
var result1 = fake.Foo(6);
var result2 = fake.Foo(-17);
var result3 = fake.Foo(7);
result1.Should().Be(12);
result2.Should().Be(289);
result3.Should().Be(0);
}
[Fact]
public void TestWithCustomExtensionMethod()
{
var fake = A.Fake<IDoSomething>();
A.CallTo(() => fake.Foo(A<int>._)).ReturnsNextFromSequenceLazily(ProduceValue1, ProduceValue2);
var result1 = fake.Foo(6);
var result2 = fake.Foo(-17);
var result3 = fake.Foo(7);
result1.Should().Be(12);
result2.Should().Be(289);
result3.Should().Be(0);
}
class MyRule : IFakeObjectCallRule
{
public MyRule(params Func<IFakeObjectCall, int>[] valueProducers)
{
this.valueProducers = new Queue<Func<IFakeObjectCall, int>>(valueProducers);
this.NumberOfTimesToCall = valueProducers.Length;
}
public int? NumberOfTimesToCall { get; init; }
Queue<Func<IFakeObjectCall, int>> valueProducers;
public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
=> fakeObjectCall.SetReturnValue(
this.valueProducers.Dequeue()(fakeObjectCall));
public bool IsApplicableTo(IFakeObjectCall fakeObjectCall) =>
fakeObjectCall.Method.GetParameters().Length == 1
&& fakeObjectCall.Method.GetParameters()[0].ParameterType == typeof(int)
&& fakeObjectCall.Method.ReturnType == typeof(int);
}
}
public static class ReturnValueConfigurationExtensions
{
public static IThenConfiguration<FakeItEasy.Configuration.IReturnValueConfiguration<T>> ReturnsNextFromSequenceLazily<T>(
this IReturnValueConfiguration<T> configuration, params Func<IFakeObjectCall, T>[] producers)
{
for (int i = 0; i < producers.Length - 1; i++)
{
configuration = configuration.ReturnsLazily(producers[i]).Once().Then;
}
return configuration.ReturnsLazily(producers[producers.Length - 1]).Once();
}
}
from fakeiteasy.
Thanks I will have a look at it next week.
All the best,
D.R.
from fakeiteasy.
Thank you, works nicely! :-) You can close it if you do not want that feature to be included in FakeItEasy by default.
Best regards,
D.R.
from fakeiteasy.
Hey, @drauch. Can you show us an example of the workaround you hate and the sample API you envision?
from fakeiteasy.
Sure, here you go:
// MY WORKAROUND:
int call = 1;
A.CallTo(() => foo.Bar()).ReturnsLazily(x =>
{
switch(call)
{
case 1: return ProduceValue1(x);
case 2: return ProduceValue2(x);
...
}
++call;
});
// ENVISIONED API:
A.CallTo(() => foo.Bar()).ReturnsNextFromSequenceLazily(ProduceValue1, ProduceValue2);
Best regards,
D.R.
from fakeiteasy.
Thanks for the feedback, @drauch, glad it worked for you!
As @blairconrad mentioned, it's not widely applicable, so we're not going to include it in FakeItEasy itself. Thanks for the suggestion anyway!
from fakeiteasy.
Related Issues (20)
- Release 8.0.1 HOT 1
- Interface type property not return created object HOT 3
- How to fake a type that have `dynamic` (ExpandoObject) properties? HOT 3
- DoesNothing() and implicit creation options throws ArgumentException HOT 5
- Release 8.1.0 HOT 1
- Include README in NuGet package HOT 1
- Silence security vulnerability complaints over Microsoft.NETCore.App 2.1.0 HOT 2
- Release 8.2.0 HOT 1
- Fake does not work as argument constraint HOT 4
- Test fails on Version 8 but succeeds on Version 7 HOT 4
- Expose caught exception(s) in protected/abstract constructors HOT 10
- Invoke method after calling an Entities public method/behaviour HOT 7
- Release 8.3.0 HOT 1
- Captured argument has empty Values HOT 6
- Document how to use InternalsVisibleTo from project files HOT 3
- InvokesAsync - possible? HOT 5
- Inconsistent results when verifying received calls HOT 12
- Adding null-valued `EventHandler`s to a fake leads to `ArgumentNullException` HOT 10
- Release vNext
- Deploy fails due to System.OverflowException when updating comments HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fakeiteasy.