Xunit 测试,创建用户

Xunit test, creating a user

嘿,我试图让这个单元测试工作,但它说,一个测试 class 可能只定义一个 public 构造函数。 但我不太确定该做什么或我做错了什么?

using Application;
using Domain.Models;
using Infrastructure;
using Infrastucture.Reposetories.Interface;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using Xunit;

namespace Tests
{
    public class UserTests
    {

        [Collection("Sequential")]
        public class Usertets
        {
            private readonly IUsersHandlers usersHandler;
            
            public Usertets(IUsersHandlers usersHandler)
            {
                this.usersHandler = usersHandler;

            }
            private readonly DB Context;

            public Usertets(DB dB)
            {
                this.Context = dB;
            }
            Guid UserID = Guid.NewGuid();
            [Fact]
            public void CreateUserTest()
            {
                usersHandler.CreateUser(UserID, "Christian", "Gerken", "christianger14@yahoo.dk", "hejsa1", "25669504", (new DateTime(2000, 1, 1)), "sofie6", 7120, "danmark", Gender.Male);


                var check = Context.Users.Where(x => x.ID == UserID).FirstOrDefault();
                Assert.NotNull(check);
            }

            [Fact]
            public void FindUser()
            {
                
                var user = usersHandler.GetUsersByID(UserID);
                Assert.NotNull(user);
            }
        }


    }
}
    

我通过优化并发使用依赖注入 对于我的 Iuserhandlers userhandler 和 dataacces 层

但是我是否缺少基于此的内容?还是我尝试了错误的单元测试?

您应该只定义一个构造函数,其中包含您需要的所有依赖项,例如:

    [Collection("Sequential")]
    public class Usertets
    {
      private readonly IUsersHandlers usersHandler;
      private readonly DB Context;

      // pass all dependencies here
      public Usertets(IUsersHandlers usersHandler, DB context)
      {
        this.usersHandler = usersHandler;
        Context = context;
      }

是这样还是我没看懂?


using Application;
using Domain.Models;
using Infrastructure;
using Infrastucture.Reposetories.Interface;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using Xunit;

namespace Tests
{
    
        [Collection("Sequential")]
        public class Usertets
        {
            private readonly IUsersHandlers usersHandler;
            
            
            private readonly DB Context;

        public interface IUsersHandlers
        {
            void CreateUser(Guid ID, string Fname, string Lname, string Email, string Password, string Phone, DateTime Birthdate, string Address, int Zip, string Country, Gender Gender);
            
        }
            public Usertets(IUsersHandlers usersHandlers, DB context)
            {
            this.usersHandler = usersHandlers;
            context = context;
            }
            Guid UserID = Guid.NewGuid();
            [Fact]
            public void CreateUserTest()
            {
                usersHandler.CreateUser(UserID, "Christian", "Gerken", "christianger14@yahoo.dk", "hejsa1", "25669504", (new DateTime(2000, 1, 1)), "sofievej6", 7100, "danmark", Gender.Male);


                var check = Context.Users.Where(x => x.ID == UserID).FirstOrDefault();
                Assert.NotNull(check);
            }

            
        }


    }

        
    ```