[英]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.