Code Monkey home page Code Monkey logo

Comments (4)

joergpichler avatar joergpichler commented on June 2, 2024 1

Hi @thomaslevesque ,

thanks that's of course the simplest fix, of course I did not think of that and fixed it by doing the comparison in the lambda function like

A.CallTo(() => extractor.Matches(context1)).ReturnsLazily((IFoo f) =>
{
    if (f == context1) return false;
    if (f == context2) return true;
    throw new InvalidOperationException();
});

To answer your other non related questions

  • I have used ReturnsLazily because I used breakpoints in there to debug the issue and never changed it back.
  • I agree that exposing a collection would be better but thats how the system was designed ¯_(ツ)_/¯

Thanks again.

from fakeiteasy.

thomaslevesque avatar thomaslevesque commented on June 2, 2024

Hi @joergpichler,

Indeed, the behavior for matching collection arguments has changed in v8. We believe that the new behavior is what people actually want in most cases, but it was inevitable that it was going to break some scenarios, unfortunately.
What you want in this case is to do a reference comparison, instead of comparing the contents of the collections:

A.CallTo(() => extractor.Matches(A<IFoo>.That.IsSameAs(context1))).ReturnsLazily(_ => false);
A.CallTo(() => extractor.Matches(A<IFoo>.That.IsSameAs(context2))).ReturnsLazily(_ => true);

(also, not sure why you're using ReturnsLazily instead of just Returns... but maybe that's just a simplified version of your code)

Just because the inheritance hierarchy includes a collection type

Well, implementing a collection interface effectively makes your type a collection. If it's intended to behave as something other than a collection, I'd consider not implementing the collection interface, but instead exposing a collection as a property or method. Obviously I don't know the context of your project, so it might not be applicable to you.

from fakeiteasy.

thomaslevesque avatar thomaslevesque commented on June 2, 2024

I was just thinking, A<IFoo>.That.IsSameAs(context1) is pretty verbose. If you use it often, you might want to declare a helper method like this:

static class Helpers
{
  public static T ByRef<T>(T value) where T : class
  {
    return A<T>.That.IsSameAs(value);
  }
}

(the name isn't great, but you can pick any name you like...)

And use it like this:

using static MyNamespace.Helpers;

...

A.CallTo(() => extractor.Matches(ByRef(context1))).ReturnsLazily(_ => false);
A.CallTo(() => extractor.Matches(ByRef(context2))).ReturnsLazily(_ => true);

One of the benefits is that it takes advantage of type inference, so you don't need to specify IFoo again.

from fakeiteasy.

blairconrad avatar blairconrad commented on June 2, 2024

@joergpichler, late-breaking option that @thomaslevesque thought of today: if switching to use A<>.That.IsSameAs() becomes annoying, you could globally disable the "if it enumerates the same, it's equal" behaviour that was turned on in FakeItEasy 8, by using a custom argument equality comparer to force comparison via ReferenceEquals.

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.