首页 > 解决方案 > 无法为在 xUnit 中执行 Entity Framework Core 操作的控制器运行测试

问题描述

我无法为在 xUnit 中执行 Entity Framework Core 操作的控制器运行测试。我正在使用内存数据库,我得到的错误是:

**A test class may only define a single public constructor.**

测试类是:

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyAppT.Controllers;
using MyAppT.Models;
using Xunit;

namespace TestingProject
{

    public class TestRegistration
    {
        #region Seeding
        protected TestRegistration(DbContextOptions<AppDbContext> contextOptions)
        {
            ContextOptions = contextOptions;
            Seed();
        }

        protected DbContextOptions<AppDbContext> ContextOptions { get; }

        private void Seed()
        {
            using (var context = new AppDbContext(ContextOptions))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var one = new Register()
                {
                    Name = "Test One",
                    Age = 40
                };

                var two = new Register()
                {
                    Name = "Test Two",
                    Age = 50
                };

                var three = new Register()
                {
                    Name = "Test Three",
                    Age = 60
                };
                context.AddRange(one, two, three);
                context.SaveChanges();
            }
        }
        #endregion

        [Fact]
        public void Test_Create_GET_ReturnsViewResultNullModel()
        {
            using (var context = new AppDbContext(ContextOptions))
            {
                // Arrange
                var controller = new RegistrationController(context);

                // Act
                var result = controller.Create();

                // Assert
                var viewResult = Assert.IsType<ViewResult>(result);
                Assert.Null(viewResult.ViewData.Model);
            }
        }
    }
}

执行 EF 核心操作的控制器是:

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

namespace MyAppT.Controllers
{
    public class RegistrationController : Controller
    {
        private AppDbContext context;
        public RegistrationController(AppDbContext appDbContext)
        {
            context = appDbContext;
        }

        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(Register register)
        {
            if (ModelState.IsValid)
            {
                context.Add(register);
                await context.SaveChangesAsync();

                return RedirectToAction("Read");
            }
            else
                return View();
        }
    }
}

运行测试时出现的奇怪错误出现在测试资源管理器中 - 测试类只能定义一个公共构造函数。

我在stackoverflow上找不到任何关于它的信息。请帮忙修复一下?

标签: asp.net-corexunit

解决方案


除非您在测试项目中使用某些 DI 框架,否则您的构造函数必须是无参数的,否则您通常不应该这样做。相反,请尝试在构造函数中创建 DBContextOptions 并将其分配给您的类变量。然后,您可以在为数据库播种以及针对它进行测试时使用它。

试试这个。Microsoft.EntityFrameworkCore.InMemory如果你还没有这个包,你需要将包添加到你的测试项目中。

public class TestRegistration
{
    #region Seeding
    public TestRegistration()
    {
        ContextOptions = new DbContextOptionsBuilder<AppDbContext>()
              .UseInMemoryDatabase(databaseName: "Test")
              .Options;
        Seed();
    }

推荐阅读