AutoFixture: Configuring an Open Generics Specimen Builder

后端 未结 2 1333
忘掉有多难
忘掉有多难 2021-01-05 04:09

I have an object model that uses Open Generics (Yes, yes, now I have two problems; that\'s why I\'m here :) :-

public interface IOGF
{
}

class C
{
         


        
2条回答
  •  余生分开走
    2021-01-05 04:24

    AFICT there are no open generics in sight. D relies on IOGF which is a constructed type.

    The error message isn't because of open generics, but because IOGF is an interface.

    You can supply a mapping from IOGF to OGF like this:

    fixture.Register>(() => fixture.CreateAnonymous>());
    

    Since OGF relies on IX you'll also need to supply a mapping to X:

    fixture.Register(() => fixture.CreateAnonymous());
    

    That should do the trick.

    However, as Nikos Baxevanis points out in his comment, if you use one of the three supplied auto-mocking extensions, this would basically work out of the box - e.g.

    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var d = fixture.CreateAnonymous();
    

提交回复
热议问题