If only you could set properties on fakes.

I'm using this library called FakeItEasy. The more I use it the more I like it.

While using it I suddenly thought:

var fakeIdentity = A.Fake<IIdentity>() { Name = "foo" };

Would be so much simpler than

var fakeIdentity = A.Fake<IIdentity>();
A.CallTo(() => fakeIdentity.Name).Returns("foo");

especially when it could be inlined without needing to introduce this fakeIdentity variable...

BUT! Obviously it doesn't work with this syntax for various reasons, like the compiler is not going to like that it is not a new statement...
Well, maybe we can't implement it, and we should just keep this in mind though for the next time we are designing a programming language. 🙂

Comments (2)

  1. Max Battcher says:

    At least one mocking framework (I don't recall which off the top of my head) allows you to provide anonymous types for initial values. Almost but not quite what you are looking for:

    var fakeIdentity = A.Fake<IIdentity>(new { Name = "foo" });

  2. tilovell09 says:

    Wait, that's almost exactly what I'm looking for! Thanks? 🙂

Skip to main content