Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

c# - CS0246 errors when I attempt to run tests

I'm trying to creat a contract test in VSCode. I have this C# test class:

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
    }
  }
}

When I run the command dotnet test I get these errors:

$ dotnet test ConsumerTest.cs(1,7): error CS0246: The type or namespace name 'xunit' could not be found (are you missing a using directive or an assembly reference?) [/Users/me/git/pact/consumer/PactTests.csproj] ConsumerTest.cs(5,31): error CS0246: The type or namespace name 'IClassFixture<>' could not be found (are you missing a using directive or an assembly reference?) [/Users/me/git/pact/consumer/PactTests.csproj] ConsumerTest.cs(7,13): error CS0246: The type or namespace name 'IMockProviderService' could not be found (are you missing a using directive or an assembly reference?) [/Users/me/git/pact/consumer/PactTests.csproj] ConsumerTest.cs(17,6): error CS0246: The type or namespace name 'FactAttribute' could not be found (are you missing a using directive or an assembly reference?) [/Users/me/git/pact/consumer/PactTests.csproj] ConsumerTest.cs(17,6): error CS0246: The type or namespace name 'Fact' could not be found (are you missing a using directive or an assembly reference?) [/Users/me/git/pact/consumer/PactTests.csproj]

This was my .csproj file:

<?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>

I added <PackageReference Include="xunit" Version="2.4.1" /> but it made no difference. How do I resolve this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...