XUnit HttpStatusCode不包含对Should的定义

时间:2018-12-05 17:27:11

标签: c# unit-testing asp.net-core .net-core

我正在编写XUnit测试用例。我收到以下错误消息

Error   CS1061  'HttpStatusCode' does not contain a definition for 'Should' and no accessible extension method 'Should' accepting a first argument of type 'HttpStatusCode' could be found (are you missing a using directive or an assembly reference?)

该错误说明了什么以及如何解决。请任何一个尝试帮助我。

谢谢..

1 个答案:

答案 0 :(得分:1)

对于Should,没有内置Should来实现类似功能Assert.Equal(HttpStatusCode.OK, defaultPage.StatusCode);

您可以尝试shouldly来简化Assert.Equal

  • 应适当安装软件包
  • 通过using Shouldly;推荐
  • 使用情况

        public async Task Test()
    {
        var server = new TestServer(WebHost.CreateDefaultBuilder()
            .UseStartup<TestStartup>()
            );
        var response = await server.CreateClient().GetAsync(@"/test");
        response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
    
        var result = await response.Content.ReadAsStringAsync();
    
    }