Mocking - can't create proxy class class?

Inside my tests, here is my code:

[SetUp]
        public void Initialise()
        {
            mockOwinManager = new Mock<IOwinManager<ApplicationUser, ApplicationRole>>();
            mockSearch = new Mock<ISearch<ApplicationUser>>();
            mockMail = new Mock<IRpdbMail>();
            mockUserStore = new Mock<IUserStore<ApplicationUser>>();

            mockOwinManager.Setup(x => x.UserManager).Returns(() => new AppUserManager(mockUserStore.Object));

            sut = new UsersController(mockOwinManager.Object, mockSearch.Object, mockMail.Object);
        }

And then the test itself:

    [Test]
    public void WhenPut_IfUserIsNullReturnInternalServerError()
    {
        //Arrange
        mockOwinManager.Setup(x => x.UserManager.FindByIdAsync(It.IsAny<string>())).Returns(() => null);

        //Act
        var response = sut.Put(new AppPersonUpdate());

        //Assert
        Assert.AreEqual(response.Result.StatusCode, HttpStatusCode.InternalServerError);
    }

But my placement string causes the following error:

Can not instantiate proxy of class: Microsoft.AspNet.Identity.UserManager`1[[SWDB.BusinessLayer.Identity.ApplicationUser, SWDB.BusinessLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].
Could not find a parameterless constructor.

Why is this so, since in the installer I set the matchOwinManager UserManager property in what I would like to return?

thanks

+4
source share
1 answer

First create a mock object UserManager. Then configure its virtual method FindByIdAsync(given that the type of the property UserManageris a class AppUserManagerand lets say that this class implements IAppUserManager).

var yourMockOfUserManager = new Mock<IAppUserManager>();
yourMockOfUserManage.Setup(x=>x.FindByIdAsync(It.IsAny<string>())).Returns(() => null);

and finally

mockOwinManager.Setup(x => x.UserManager).Returns(() => yourMockOfUserManager.Object);
+2
source

Source: https://habr.com/ru/post/1621713/


All Articles