简体   繁体   English

我应该向控制器传递什么才能在 ASP.NET Core 2.0 中执行模型单元测试

[英]What should I pass to controller in order to perform model unit testing in ASP.NET Core 2.0

How to perform model unit testing in .net core 2.0?如何在 .net core 2.0 中执行模型单元测试? I am not getting what to pass to Homecontroller class in unit testing class.我没有得到在单元测试类中传递给 Homecontroller 类的内容。 This is my unit testing class where I need help这是我需要帮助的单元测试课程

在此处输入图片说明

using BasicUnitTesting.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BasicUnitTesting.Controllers;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace BasicUnitTestProj
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void GetEmployeeTest()
        {
            HomeController home = new HomeController(null); //What should i pass to HomeController

            var vr = home.GetEmployee() as ViewResult;

            List<Employee> expectedemps = new List<Employee>()
            {
                new Employee() {EmpID=101,EmpName="Abhilash",Salary=50000},
                new Employee() {EmpID=102,EmpName="Abhishek",Salary=60000},
                new Employee() {EmpID=103,EmpName="Nagraj",Salary=30000},
                new Employee() {EmpID=104,EmpName="Sunil",Salary=50000},
                new Employee() {EmpID=105,EmpName="Vinay",Salary=40000}
            };

            Assert.IsTrue(expectedemps.SequenceEqual(vr.Model as List<Employee>));
        }
    }
}

Below is my Homecontroller下面是我的家庭控制器

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BasicUnitTesting.Models;

namespace BasicUnitTesting.Controllers
{
    public class HomeController : Controller
    {
        private readonly CompanyDbContext _context

        public HomeController(CompanyDbContext context)
        {
            _context = context;
        }

        public IActionResult Index()
        {
            return View("Index");
        }

        public IActionResult GetEmployee()
        {
            //CompanyDbContext Db = new CompanyDbContext();
            //List<Employee> emps = Db.Employees.ToList();
            List<Employee> emps = _context.Employees.ToList();
            return View(emps);
        }         
    }
}

My model and Context class are as follows:我的模型和上下文类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

namespace BasicUnitTesting.Models
{
    public class Employee
    {
        [Key]
        public int EmpID { get; set; }
        public string EmpName { get; set; }
        public decimal Salary { get; set; }
    }

    public class CompanyDbContext:DbContext
    {
        public CompanyDbContext()
        {
        }

        public CompanyDbContext(DbContextOptions<CompanyDbContext> options) : base(options)
        {
        }

        public DbSet<Employee> Employees { get; set; }
    }
}

You should pass abstraction/interface to your Controller, it should not know about details/implementation of your DbContext.你应该将抽象/接口传递给你的控制器,它不应该知道你的 DbContext 的细节/实现。

What you need is another Interface/Contract:您需要的是另一个接口/合同:

public interface ICompanyDbContext
{
   DbSet<Employee> Employees { get; set; }
}

and your class should implement this interface:你的类应该实现这个接口:

public class CompanyDbContext: ICompanyDbContext, DbContext

Your controller should have argument of ICompanyDbContext not CompanyDbContext:你的控制器应该有 ICompanyDbContext 的参数而不是 CompanyDbContext:

public HomeController(ICompanyDbContext context)

This allows you to mock ICompanyDbContext in your unit test:这允许您在单元测试中模拟 ICompanyDbContext:

var dbContext = new Mock<ICompanyDbContext>();
// you should SetUp mock as well as do verification on it

HomeController home = new HomeController(dbContext.Object); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC 4单元测试(我应该测试什么) - ASP.NET MVC 4 Unit testing (what i should test) 具有授权ASP.NET Core 2.0 Web API的单元测试控制器 - Unit Testing Controller with Authorization ASP.NET Core 2.0 Web API 在ASP.NET Core MVC API控制器上单元测试AuthorizeAttribute - Unit testing an AuthorizeAttribute on an ASP.NET Core MVC API controller 控制器中Asp.Net核心单元测试删除方法 - Asp.Net Core Unit Testing Delete Method in controller ASP.NET Core 2.1:我应该使用什么技术来发布具有列表属性的模型? - ASP.NET Core 2.1: What technique should I use to post a model that have a property that is a list? ASP.NET Core - 模型绑定测试 - ASP.NET Core - Model binding testing ASP.NET 核心单元测试抛出 Null 异常时测试 controller 问题响应 - ASP.NET Core unit test throws Null Exception when testing controller problem response 单元测试 - 控制器没有从ASP.NET Core Web API项目中的模拟IConfiguration中获取值 - Unit Testing - Controller not picking up value from mocked IConfiguration in ASP.NET Core Web API Project 在单元测试ASP.NET Core控制器时如何正确模仿IAuthenticationHandler - How to properly mimic IAuthenticationHandler while unit testing ASP.NET Core controller 单元测试 ASP.NET 核心 3.1 MVC controller - 值不能是 null。 (参数“提供者”) - Unit testing ASP.NET Core 3.1 MVC controller - value cannot be null. (Parameter 'provider')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM