net core 2.0 xunit Controller with dependency injection using Mock is null

試著忘記壹切 提交于 2020-03-25 12:32:14

问题


I am doing an Net Core 2.0 Rest Api application.

I am using dependency injection with Interface IContactBiz and class ContactBiz.

IContactBiz is defined like this

public interface IContactBiz
{
    ReturnModel Add(List<ContactEntity> lstContactEntity, ContactEntity contact); 
}

My ContactBiz class

public class ContactBiz: IContactBiz
{   
    public ReturnModel Add(List<ContactEntity> lstContactEntity, ContactEntity contact)
    {
        contact.ID = Guid.NewGuid().ToString();
        lstContactEntity.Add(contact);
        return new ReturnModel()
        {
            Result = true,
        };
     }
}

It is set in startup.cs

public void ConfigureServices(IServiceCollection services)
{  
    services.AddSingleton<IContactBiz, ContactBiz>();
}

I have a ContactController, defined like this

public class ContactController : Controller
{
    private readonly IContactBiz _contactBiz;

    public ContactController(IContactBiz contactbiz)
    {
        _contactBiz = contactbiz;
    }

   //...

I have Post Method on my controller where i call Add() method from ContactBiz class

[HttpPost]
public IActionResult Post([FromBody]ContactModel contact)
{
    ReturnModel errorModel = new ReturnModel();

    //...

    errorModel = _contactBiz.Add(lstContactEntity, contactEntity);

    //...
 }

When I run it with Postman, it works fine.. I reach Add() method from ContactBiz.cs and retrieve data.

But I have a xUnit project to test it, and I want to use Moq..

I have

public class ContactControllerTest
{
    ContactController _controller;
    public ContactControllerTest()
    {
        Mock<IContactBiz> mockRepo = new Mock<IContactBiz>();
        _controller = new ContactController(contactbiz: mockRepo.Object);
    }

    [Fact]
    public void InsertContact()
    {
        ContactModel cm = new ContactModel()
        {
            Address = "data",
            City = "data",
            Company = "data",
            Email = "data",
            Name = "data",
            PhonePersonal = "data",
            PhoneWork = "data",
            State = "data",
            BirthDate = DateTime.Today.AddYears(-30)
        };
        var actionResult = _controller.Post(cm);
    }
}

I run it in debug mode...

When I reach the line errorModel = _contactBiz.Add(lstContactEntity, contactEntity); in Post() method.. is null...

Add() method from the class ContactBiz it is never called

I think i am missing to set which class has is mapped to _contactBiz interface

the line I have in startup.cs

services.AddSingleton<IContactBiz, ContactBiz>(); Do not have access to Add() method por ContactBiz class, and errorModel is null.

I think the problem is that I did not I assign services.AddSingleton<IContactBiz, ContactBiz>(); in Moq?

Is there another problem?

Thanks


回答1:


Configure the mock to behave as expected when invoked

ReturnModel returnModel = new ReturnModel() {
    Result = true,
};
Mock<IContactBiz> mockRepo = new Mock<IContactBiz>();
mockRepo
    .Setup(_ => _.Add(It.IsAny<List<ContactEntity>>(), It.IsAny<ContactEntity>()))
    .Return(returnModel);

_controller = new ContactController(contactbiz: mockRepo.Object);

otherwise it will return the default value of the return type



来源:https://stackoverflow.com/questions/60747253/net-core-2-0-xunit-controller-with-dependency-injection-using-mock-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!