18

Let's say I have interface:

public interface IFoo
{
    int Bar1 { get; set; }
    int Bar2 { get; set; }
}

If IFoo was class, I could write:

fixture.CreateAnonymous<IFoo>();

and the result will have numbers set for Bar1 and Bar2.

But how to do this with interface? I tried to use AutoMoqCustomization but this seems to be for properties with interface type and not interfaces itself.

I am looking for automated way like CreateAnonymous is for classes. Currenlty I am creating interface mock and setuping it's properties explicitly which is work I would like to save. I must missing something obvious.

Pol
  • 5,064
  • 4
  • 32
  • 51
  • 2
    Here's how to make that work: http://blog.ploeh.dk/2013/04/08/how-to-automatically-populate-properties-with-automoq – Mark Seemann Jun 01 '16 at 10:42

1 Answers1

17

If you want to map an interface to a specific concrete class, you can certainly do that:

fixture.Register<IFoo>(() => fixture.CreateAnonymous<ConcreteFoo>());

(Or fixture.Register<IFoo>( fixture.CreateAnonymous<ConcreteFoo>) for short)

However, AutoMoq (as well as AutoRhinoMocks and AutoFakeItEasy) is also an option. With that, an attempt to create an instance of IFoo will return a Moq-created proxy which implements IFoo.

However, with Moq you aren't going to see the Bar1 and Bar2 populated. That's not only because AutoFixture doesn't invoke the setters, but because Moq doesn't (by default) implement the getters.

In order to make that work for Moq, one needs to invoke SetupAllProperties() on the Mock<T> itself. While possible, that's a little bit difficult to do in the current AutoMoq graph. There's already a work item for this, but if you read through the discussion you'll see that the issue is more complex than it would seem.

In any case, interfaces with properties is a bad idea for a number of other reasons too, so the best solution is to redesign the interface so that it doesn't have properties.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 4
    "interfaces with properties is a bad idea" - Always? If so could you give me a link? :) – Pol Apr 18 '12 at 12:33
  • 2
    Lots of reasons. The Law of Demeter is one of them: http://en.wikipedia.org/wiki/Law_of_Demeter Let me ask something in return: what benefit does an interface with properties provide? – Mark Seemann Apr 18 '12 at 12:45
  • I am familiar with Law of Demeter. I use properties (in 90% cases read-only) for entities but only for some subset of data, just because of Law of Demeter not for all data. Still, sometimes they are useful. – Pol Apr 18 '12 at 12:49
  • But an Entity is a concrete type, right? How does an interface enter the picture? – Mark Seemann Apr 18 '12 at 13:46
  • Repository returns interface, not concrete type. – Pol Apr 18 '12 at 14:05
  • Why? What benefit does it provide? Do you have more then one implementations of each Entity? – Mark Seemann Apr 19 '12 at 20:39
  • Clarity. I like to open an interface to look what I can do with type. – Pol May 06 '12 at 14:18
  • 2
    The public API of a concrete type will tell you that just as well. – Mark Seemann May 06 '12 at 19:59
  • 3
    In general, I couldn't agree more, I'm all for clean code itself, but my reasons are a bit subjective. Anyway, I had made CreateAnonymousMock extension method and I'm using it for few weeks now, without problem: http://pol84.tumblr.com/post/23032897078/autofixture-and-interfaces – Pol May 14 '12 at 16:25
  • That's just what I was looking for - except Ploeh broke it in https://github.com/AutoFixture/AutoFixture/issues/61 I guess I'll have to look further :) – increddibelly Jun 01 '16 at 09:28
  • @increddibelly I've added a comment with a link to the OP itself. HTH. – Mark Seemann Jun 01 '16 at 10:42
  • @MarkSeemann Fundamentally, aren't properties simply syntactically sugared get_x() and set_x() methods? How does this breaks the Law of Demeter? – bkqc Jan 18 '22 at 20:36
  • @bkqc The Law of Demeter concerns itself with how objects expose their internal dependencies. Whether that exposure happens via a class field or a method isn't particularly relevant. – Mark Seemann Jan 19 '22 at 11:17
  • Taking back the dog example, what is wrong with having an IDog{ ICollar Collar { get; set; } int LegsCount { get; }} ? I suppose I misunderstand the law... Sorry, I wasn't schooled to it ;) – bkqc Jan 19 '22 at 17:17
  • @bkqc I can't answer that in a comment, but have you read [the Wikipedia article](https://en.wikipedia.org/wiki/Law_of_Demeter)?. FWIW, the _animal_ domain is a terrible problem domain for teaching object-oriented programming. I wish schools would stop using those examples. – Mark Seemann Jan 19 '22 at 19:54