Giter Club home page Giter Club logo

Comments (6)

blairconrad avatar blairconrad commented on June 26, 2024 1

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.

drauch avatar drauch commented on June 26, 2024 1

Thanks I will have a look at it next week.

All the best,
D.R.

from fakeiteasy.

drauch avatar drauch commented on June 26, 2024 1

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.

blairconrad avatar blairconrad commented on June 26, 2024

Hey, @drauch. Can you show us an example of the workaround you hate and the sample API you envision?

from fakeiteasy.

drauch avatar drauch commented on June 26, 2024

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.

thomaslevesque avatar thomaslevesque commented on June 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.