本文介绍了使用Mock进行依赖项注入的net core 2.0 xunit Controller为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Net Core 2.0 Rest Api应用程序.

I am doing an Net Core 2.0 Rest Api application.

我正在使用接口IContactBiz和类ContactBiz的依赖项注入.

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

IContactBiz的定义如下

IContactBiz is defined like this

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

我的ContactBiz类

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,
        };
     }
}

它是在startup.cs中设置的

It is set in startup.cs

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

我有一个如此定义的ContactController

I have a ContactController, defined like this

public class ContactController : Controller
{
    private readonly IContactBiz _contactBiz;

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

   //...

我的控制器上有Post Method,我从ContactBiz类调用Add()方法

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);

    //...
 }

当我用Postman运行它时,它工作正常..我从ContactBiz.cs到达Add()方法并检索数据.

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

但是我有一个xUnit项目要对其进行测试,并且我想使用最小起订量.

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

我有

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...

当我到达线路时Post()方法中的errorModel = _contactBiz.Add(lstContactEntity, contactEntity);为空...

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

ContactBiz类中的

Add()方法从不被调用

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

我想我缺少设置哪个类已映射到_contactBiz接口

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

startup.cs中的行

the line I have in startup.cs

services.AddSingleton<IContactBiz, ContactBiz>();无法访问ContactBiz类的Add()方法,并且errorModel为空.

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

我认为问题是我没有在Moq中分配services.AddSingleton<IContactBiz, ContactBiz>();吗?

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

还有其他问题吗?

谢谢

推荐答案

将模拟程序配置为在调用时表现出预期的行为

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

这篇关于使用Mock进行依赖项注入的net core 2.0 xunit Controller为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:37