Search code examples
c#unit-testingmicrosoft-fakes

Stub generic Interface with MS Fakes


How to create a Stub IRepository<TModel>? I have 3 interfaces:

namespace DataAccessLayer.Repository.Interfaces 
{
    public interface IRepository<TModel> : IDisposable {...}
    public interface ICustomerRepository : IRepository<CustomerModel> {...}
    public interface IRepositoryContainer {...}
}

My DataAccessLayer.fakes looks as follows:

<StubGeneration>
    <Clear/>
    <Add FullName="DataAccessLayer.Repository.Interfaces.IRepositoryContainer!"/>
    <Add FullName="DataAccessLayer.Repository.Interfaces.IRiadRepository!"/>
    <Add FullName="DataAccessLayer.Repository.Interfaces.IRepository!"/>
</StubGeneration>

In my unit tests I can see StubIRepositoryContainer and also ICustomerRepository, but stub for IRepository<TModel> is not generated.

Thanks.


Solution

  • I think your name filtering is too limited. Try removing the '!' from IRepository line in your fakes configuration file.

    <Add FullName="DataAccessLayer.Repository.Interfaces.IRepository"/>
    

    That raises the question if the syntax will allow filtering with the '!'. The couple of variations I tried did not work ("IRepository!TModel"). Here's some information about Parameter Naming Type Conventions.