How to mock a class that implements multiple interfaces

loyalflow picture loyalflow · Apr 4, 2013 · Viewed 24.6k times · Source

How to mock the following class:

UserRepository : GenericRepository<User>, IUserRepository


public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class

I am using Moq, and I am confused how to handle multiple interfaces correctly.

Answer

ShloEmi picture ShloEmi · Jan 26, 2015

Take a look at https://github.com/Moq/moq4/wiki/Quickstart

Advanced Features

// implementing multiple interfaces in mock
var foo = new Mock<IFoo>();
var disposableFoo = foo.As<IDisposable>();
// now IFoo mock also implements IDisposable :)
disposableFoo.Setup(df => df.Dispose());