1

Currently i am writing unit tests for repository, wrapping linq2db methods. I am trying to mock a simple select method but of course you can't mock extension methods like GetTable() or Insert(item).

What i am currently trying to do is set up a simple test using Moq, AutoFixture and Autofac

using (var mock = AutoMock.GetLoose())
{
     var fixture = new Fixture();
     var customers = fixture
        .Build<List<Customer>>()
        .Create();

     mock.Mock<IDataContext>()
        .Setup(item => item.GetTable<Customer>().ToList())
        .Returns(customers);

     var cls = mock.Create<CustomerRepository>();

     var actual = cls.GetAllAsync().Result;

     Assert.IsTrue(actual != null);
     Assert.Equals(customers.Count, actual.Count());
}

The test of course fails at Setup, since GetTable<Customer>() is an extension method of IDataContext.

How do you properly test a repository wrapping linq2db?

4

0 回答 0