Moq DbContext throw Castle.DynamicProxy error


I have a service class in .NET Core whose contractor is expected to pass with an instance of DbContext. I am using xUnit with Moq and Feature. In test class constructor I am getting Castle Dynamic Proxy error

   Message=Can not instantiate proxy of class: myApp.Data.Context.SupplierContext.
 Could not find a parameterless constructor.
   at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
  at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
 at Moq.CastleProxyFactory.CreateProxy(Type mockType, IInterceptor interceptor, Type[] interfaces, Object[] arguments)
 at Moq.Mock`1.InitializeInstance()
 at Moq.Mock`1.OnGetObject()
 at Moq.Mock.get_Object()
 at Moq.Mock`1.get_Object()

Service class

public class SupplierService : ISupplierService
    private readonly SupplierContext _context;

    public SupplierService(SupplierContext context)
        _context = context;

test class

public class SupplierServiceTest
    private readonly ISupplierService sut;
    private readonly Mock<SupplierContext> mockSupplierContext = new Mock<SupplierContext>();

    public SupplierServiceTest()
        sut = new SupplierService(mockSupplierContext.Object); // get error here....

    public void GetSuppliers_ShouldReturnAllSuppliers()
        var fixture = new Fixture();
        var supplierDataMock = fixture.CreateMany<Task<Supplier>>(6);

        var actualDataResult = sut.GetSuppliers();

        //Assert.Equal(supplierDataMock, actualDataResult);
asked on Stack Overflow Nov 3, 2020 by Toxic

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0