Xunit Test - 类型或命名空间''无法找到。

问题描述 投票:0回答:1

首先对不起我的英语。我对编程很陌生,开始在Pluralsight看Scott Allen的C#基础知识。我在Xunit Testing遇到了一个障碍,我试图在测试proj上检索一个类,但它一直在说类类型或命名空间无法找到.我已经从测试proj添加了一个引用到主proj,并确保他们的目标是相同的框架,但我仍然得到相同的错误。

试着在测试文件中添加使用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="..\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;


                }



c# xunit
1个回答
1
投票

你有 Book 阶层 内部Program 类。

将所有的班级移动到 Program 外界 partial Program class 的地方 namespace Gradebook.

© www.soinside.com 2019 - 2024. All rights reserved.