缺少using指令

时间:2019-07-09 12:21:45

标签: c# directive using

C#的新手,正在尝试编写测试程序。 但是,出现以下错误:

  

TypeTests.cs(30,41):错误CS0246:类型或名称空间名称“书”   找不到(您是否缺少using指令或程序集   参考?)   [C:\ dev \ gradbook \ test \ GradeBook.tests \ GradeBook.tests.csproj]   TypeTests.cs(45,37):错误CS0246:类型或名称空间名称“书”   找不到(您是否缺少using指令或程序集   参考?)   [C:\ dev \ gradbook \ test \ GradeBook.tests \ GradeBook.tests.csproj]   TypeTests.cs(86,9):错误CS0246:类型或名称空间名称“ book”   找不到(您是否缺少using指令或程序集   参考?)   [C:\ dev \ gradbook \ test \ GradeBook.tests \ GradeBook.tests.csproj]

尝试重建并重新添加xunit

using System;
using Xunit;

namespace GradeBook.tests
{
    public class TypeTests
    {
         [Fact]
         public void Test1() 
         {
             var x = GetInt();

             Assert.Equal(3, x);
         }

         private int GetInt() 
         {
             return 3;
         }

         [Fact]
        public void CSharpCanPassByRef()
        {
            var book1 = GetBook("Book 1");
            GetBookSetName(ref book1, "New Name");

            Assert.Equal("Book 1", book1.name);

        }
        private void GetBookSetName(ref Book book, string name) 
        {
            book = new Book(name); 
        }

         [Fact]
        public void CSharpIsPassByValue()
        {
            var book1 = GetBook("Book 1");
            GetBookSetName(book1, "New Name");

            Assert.Equal("Book 1", book1.name);

        }

        private void GetBookSetName(Book book, string name) 
        {
            book = new Book(name);

        }
        [Fact]
        public void CanSetNameFromReference()
        {
            var book1 = GetBook("Book 1");
            SetName(book1, "New Name");

            Assert.Equal("New Name", book1.name);

            }

        private void SetName(BookTests book, string name) 
        {
            book.Name = name;
        }
        [Fact]
        public void GetBookReturnDifferentObject() 
        {
            var book1 = GetBook("Book 1");
            var book2 = GetBook("Book 2");

            Assert.Equal("Book1", book1.name);
            Assert.Equal("Book2", book2.name);
            Assert.NotSame(book1, book2);
        }


        [Fact]
        public void TwoVarsCanReferenceSameObject()
        {
            var book1 = GetBook("Book 1");
            var book2 = book1;

            Assert.Same(book1, book2);
            Assert.True(Object.ReferenceEquals(book1, book2));
        }

        Book GetBook(string name) 
        {
            return new Book(name);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

要解决此问题,您需要引用您的项目

  1. 在您的终端上添加 dotnet添加参考

示例

dotnet add reference <PROJECT> ..\..\src\GradeBook\GradeBook.csproj
  1. 检查您的cs文件并查看创建类public class Book的名称空间
  2. 如果命名空间使用GradeBook,则您的namespace GradeBook.tests需要更改为namespace GradeBook

答案 1 :(得分:0)

对我有用。我从名称空间GradeBook.Tests更改为名称空间Gradebook