Moq DbContext throw Castle.DynamicProxy error

0

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

 Castle.DynamicProxy.InvalidProxyConstructorArgumentsException
   HResult=0x80070057
   Message=Can not instantiate proxy of class: myApp.Data.Context.SupplierContext.
 Could not find a parameterless constructor.
 Source=Castle.Core
 StackTrace:
   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....
    }

    [Fact]
    public void GetSuppliers_ShouldReturnAllSuppliers()
    {
        //Arrange
        var fixture = new Fixture();
        var supplierDataMock = fixture.CreateMany<Task<Supplier>>(6);

        //Act
        var actualDataResult = sut.GetSuppliers();

        //Assert
        //Assert.Equal(supplierDataMock, actualDataResult);
    }
unit-testing
asp.net-web-api
moq
xunit
.net-core-2.2
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