xUnit 测试 - 找不到类型或命名空间“”

xUnit Test - Type or Namespace ' ' could not be found

我是编程新手,开始在 Pluralsight 上观看 Scott Allen 的 C# 基础知识。我遇到了 xUnit 测试的障碍。当试图在测试项目上检索 class 时,它一直说找不到 class 类型或命名空间。 我已经将测试项目的引用添加到主项目,并确保它们针对相同的框架,但我仍然遇到相同的错误。

尝试使用成绩簿添加; /成绩簿;在测试文件上,但它是灰色的。

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
  </ItemGroup>

</Project>

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Gradebook\Gradebook.csproj" />
  </ItemGroup>

</Project>

测试文件:

using Xunit;

namespace GradeBook.Tests
{
    public class BookTests
    {
        [Fact]
        public void Test1()
        {
            var book = new Book(""); /// **'Book' type or namespace could not be found**
        }
    }
}

Book.cs

using System;
using System.Collections.Generic;
    
namespace Gradebook
{
    partial class Program
    {    
        public class Book
        {  
            //Initializes grade field and labels the list with unique name.
    
            private List<double> grades;
            private string name;
    
            public Book(string name)
            {    
                grades = new List<double>();
                this.name = name;    
            }    
    
            public void AddGrade(double grade)
            {
                grades.Add(grade);
            }  
    
            //shows the average grade, highest/lowest grade in a Book.
    
            public Statistics GetStatistics() 
            {
                Statistics result = new Statistics();
    
                result.Average = 0.0;
    
                result.High = double.MinValue;
                result.Low = double.MaxValue;  
    
                foreach (double grade in grades)
                {    
                    result.High = Math.Max(grade, result.High);
                    result.Low = Math.Min(grade, result.High);
    
                    result.Average += grade;  
                }
    
                result.Average /= grades.Count;
    
                return result;  
            }  
        }
    }

您有 Book class INSIDE Program class。

Program 内的所有 class 移动到 partial Program class 外并放在 namespace Gradebook.