x单元测试 - 找不到类型或命名空间



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

已尝试使用Gradebook;/GradeBook;在测试文件上添加,但它显示为灰色。

<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="..GradebookGradebook.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**
}
}
}

书.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;  
}  
}
}

Program中有Book类。

将所有类移到Programpartial Program class外,并放置在namespace Gradebook下。

最新更新