Visual Studio中的xUnit未运行

8aqjt8rx  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(224)

我是新的xUnit与.NET核心。我试图运行测试的一个示例项目在VS 2022。这是我的测试类:

using DeskBooker.Core.Domain;
using DeskBooker.Core.Processor;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Moq;
using System;
using System.Collections.Generic;
using Xunit;

namespace DeskBooker.Web.Pages
{
 public class BookDeskModelTests
 {
   private Mock<IDeskBookingRequestProcessor> _processorMock;
   private BookDeskModel _bookDeskModel;
   private DeskBookingResult _deskBookingResult;

   public BookDeskModelTests()
   {
     _processorMock = new Mock<IDeskBookingRequestProcessor>();

     _bookDeskModel = new BookDeskModel(_processorMock.Object)
     {
       DeskBookingRequest = new DeskBookingRequest()
     };

     _deskBookingResult = new DeskBookingResult
     {
       Code = DeskBookingResultCode.Success
     };

     _processorMock.Setup(x => x.BookDesk(_bookDeskModel.DeskBookingRequest))
      .Returns(_deskBookingResult);
   }

   [Theory]
   [InlineData(1, true)]
   [InlineData(0, false)]
   public void ShouldCallBookDeskMethodOfProcessorIfModelIsValid(
     int expectedBookDeskCalls, bool isModelValid)
   {
     // Arrange
     if (!isModelValid)
     {
       _bookDeskModel.ModelState.AddModelError("JustAKey", "AnErrorMessage");
     }

     // Act
     _bookDeskModel.OnPost();

     // Assert
     _processorMock.Verify(x => x.BookDesk(_bookDeskModel.DeskBookingRequest),
       Times.Exactly(expectedBookDeskCalls));
   }

   [Fact]
   public void ShouldAddModelErrorIfNoDeskIsAvailable()
   {
     // Arrange
     _deskBookingResult.Code = DeskBookingResultCode.NoDeskAvailable;

     // Act
     _bookDeskModel.OnPost();

     // Assert
     var modelStateEntry =
       Assert.Contains("DeskBookingRequest.Date", _bookDeskModel.ModelState);
     var modelError = Assert.Single(modelStateEntry.Errors);
     Assert.Equal("No desk available for selected date", modelError.ErrorMessage);
   }

   [Fact]
   public void ShouldNotAddModelErrorIfDeskIsAvailable()
   {
     // Arrange
     _deskBookingResult.Code = DeskBookingResultCode.Success;

     // Act
     _bookDeskModel.OnPost();

     // Assert
     Assert.DoesNotContain("DeskBookingRequest.Date", _bookDeskModel.ModelState);
   }

   [Theory]
   [InlineData(typeof(PageResult), false, null)]
   [InlineData(typeof(PageResult), true, DeskBookingResultCode.NoDeskAvailable)]
   [InlineData(typeof(RedirectToPageResult), true, DeskBookingResultCode.Success)]
   public void ShouldReturnExpectedActionResult(Type expectedActionResultType,
     bool isModelValid, DeskBookingResultCode? deskBookingResultCode)
   {
     // Arrange
     if (!isModelValid)
     {
       _bookDeskModel.ModelState.AddModelError("JustAKey", "AnErrorMessage");
     }

     if (deskBookingResultCode.HasValue)
     {
       _deskBookingResult.Code = deskBookingResultCode.Value;
     }

     // Act
     IActionResult actionResult = _bookDeskModel.OnPost();

     // Assert
     Assert.IsType(expectedActionResultType, actionResult);
   }

   [Fact]
   public void ShouldRedirectToBookDeskConfirmationPage()
   {
     // Arrange
     _deskBookingResult.Code = DeskBookingResultCode.Success;
     _deskBookingResult.DeskBookingId = 7;
     _deskBookingResult.FirstName = "Thomas";
     _deskBookingResult.Date = new DateTime(2020, 1, 28);

     // Act
     IActionResult actionResult = _bookDeskModel.OnPost();

     // Assert
     var redirectToPageResult = Assert.IsType<RedirectToPageResult>(actionResult);
     Assert.Equal("BookDeskConfirmation", redirectToPageResult.PageName);

     IDictionary<string, object> routeValues = redirectToPageResult.RouteValues;
     Assert.Equal(3, routeValues.Count);

     var deskBookingId = Assert.Contains("DeskBookingId", routeValues);
     Assert.Equal(_deskBookingResult.DeskBookingId, deskBookingId);

     var firstName = Assert.Contains("FirstName", routeValues);
     Assert.Equal(_deskBookingResult.FirstName, firstName);

     var date = Assert.Contains("Date", routeValues);
     Assert.Equal(_deskBookingResult.Date, date);
   }
 }
}

字符串
这是我的测试项目设置:

<Project Sdk="Microsoft.NET.Sdk">

 <PropertyGroup>
   <TargetFramework>net5.0</TargetFramework>
   <IsPackable>false</IsPackable>
   <RootNamespace>DeskBooker.Web</RootNamespace>
   <Platforms>AnyCPU;x64</Platforms>
 </PropertyGroup>

 <ItemGroup>
   <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
   <PackageReference Include="Moq" Version="4.13.1" />
   <PackageReference Include="xunit" Version="2.4.1" />
   <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
   <PackageReference Include="coverlet.collector" Version="1.1.0" />
 </ItemGroup>

 <ItemGroup>
   <ProjectReference Include="..\DeskBooker.Web\DeskBooker.Web.csproj" />
 </ItemGroup>

</Project>


然而,当我运行测试时,它只构建,但不运行任何测试,因为它似乎没有发现任何测试。
怎么了?
谢谢

kxkpmulp

kxkpmulp1#

问题似乎是.NET TargetFramework太旧了。

相关问题