我尝试运行测试时出现 CS0246 错误

时间:2021-01-05 10:58:12

标签: c# .net visual-studio-code cs0246

我正在尝试在 VSCode 中创建合同测试。我有这个 C# 测试类:

using xunit;

namespace PactTests.PactTests
{
  public class ConsumerTest : IClassFixture<ConsumerPact>
  {
    private IMockProviderService _mockProviderService;
    private string _mockProviderServiceBaseUri;

    public ConsumerTest(ConsumerPact data)
    {
      _mockProviderService = data.MockProviderService;
      _mockProviderService.ClearInteractions(); //NOTE: Clears any previously registered interactions before the test is run
      _mockProviderServiceBaseUri = data.MockProviderServiceBaseUri;
    }

    [Fact]
    public void OKResponse()
    {
      //Arrange
      _mockProviderService
        .Given("user token")
        .UponReceiving("GET user token")
        .With(new ProviderServiceRequest
        {
          Method = HttpVerb.Get,
          Path = "/token/1234",
          Headers = new Dictionary<string, object>
          {
            { "application/json, text/plain, */*" }
          }
        })
        .WillRespondWith(new ProviderServiceResponse
        {
          Status = 200,
          Headers = new Dictionary<string, object>
          {
            { "Content-Type", "application/json" }
          },
          Body = new //NOTE: Note the case sensitivity here, the body will be serialised as per the casing defined
          {
            token = "bearer"
          }
        }); //NOTE: WillRespondWith call must come last as it will register the interaction

        var consumer = new SomethingApiClient(_mockProviderServiceBaseUri);

      //Act
      var result = consumer.GetSomething("tester");

      //Assert
      Assert.Equal("tester", result.id);

      _mockProviderService.VerifyInteractions(); //NOTE: Verifies that interactions registered on the mock provider are called at least once
    }
  }
}

当我运行命令 dotnet test 时,出现以下错误:

<块引用>

$ dotnet 测试 ConsumerTest.cs(1,7):错误 CS0246:找不到类型或命名空间名称“xunit”(您是否缺少 using 指令或程序集引用?)[/Users/me/git/pact/consumer/PactTests .csproj] ConsumerTest.cs(5,31):错误 CS0246:找不到类型或命名空间名称“IClassFixture<>”(您是否缺少 using 指令或程序集引用?)[/Users/me/git/pact/consumer /PactTests.csproj] ConsumerTest.cs(7,13):错误 CS0246:找不到类型或命名空间名称“IMockProviderService”(您是否缺少 using 指令或程序集引用?)[/Users/me/git/pact/consumer/PactTests .csproj] ConsumerTest.cs(17,6):错误 CS0246:找不到类型或命名空间名称“FactAttribute”(您是否缺少 using 指令或程序集引用?)[/Users/me/git/pact/consumer/PactTests .csproj] ConsumerTest.cs(17,6):错误 CS0246:找不到类型或命名空间名称“Fact”(您是否缺少 using 指令或程序集引用?)[/Users/me/git/pact/consumer/PactTests .csproj]

这是我的 .csproj 文件:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
    <PackageReference Include="Microsoft.VisualStudio.TestPlatform" Version="14.0.0.1" />
    <PackageReference Include="NUnit" Version="3.10.1" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
    <PackageReference Include="PactNet" Version="2.0.17" />
    <PackageReference Include="PactNet.OSX" Version="2.0.17" />
  </ItemGroup>
</Project>

我添加了 <PackageReference Include="xunit" Version="2.4.1" /> 但它没有任何区别。我该如何解决?

0 个答案:

没有答案