由于单元测试而面试失败-我需要了解原因[关闭]

Posted

技术标签:

【中文标题】由于单元测试而面试失败-我需要了解原因[关闭]【英文标题】:Failed an interview due to unit test - I need to learn why [closed] 【发布时间】:2021-10-30 02:03:45 【问题描述】:

我去面试,给了一个NUnit测试,失败了,问为什么失败,找不到原因。在调试过程中,除了 ID 分别为 1 和 0 之外,其他值都相同。

我的测试能力不强,但想了解我的错误,我要注意什么,为什么会失败?

我在调试模式下运行它并得到第一个错误,但没有看到问题所在。这个单元测试出了什么问题。我被要求修复它,但在规定的时间内我找不到失败的原因

错误信息

NUnit.Framework.AssertionException


HResult=0x80131500
  Message=Expected _context.Account Root.CLIENTBooking.Data.Models.Account
   
      Created = <2021-08-31 07:58:18.8245439>
      DateOfBirth = <2021-10-26 02:51:07.9281209>
      Email = "Email44cc0cbb-a6c3-43a5-974d-e1003b958854"
      FirstName = "FirstNameda3ad7fd-f2d4-4720-92df-46f82b6406cd"
      Gender = 0
      Id = 1L
      LastName = "LastName765872d6-7fe9-475a-b217-87c45ca7e1a6"
      Orders = empty
    to contain equivalent of 

Root.CLIENTBooking.Data.Models.Account

   Created = <2021-08-31 07:58:04.4695485>
   DateOfBirth = <2021-10-26 02:51:07.9281209>
   Email = "Email44cc0cbb-a6c3-43a5-974d-e1003b958854"
   FirstName = "FirstNameda3ad7fd-f2d4-4720-92df-46f82b6406cd"
   Gender = 0
   Id = 0L
   LastName = "LastName765872d6-7fe9-475a-b217-87c45ca7e1a6"
   Orders = empty
.

With configuration:
- Use declared types and members
- Compare enums by value
- Exclude member root.Id
- Match member by name (or throw)
- Without automatic conversion.
- Be strict about the order of items in byte arrays

  Source=FluentAssertions
  StackTrace:
   at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message)
   at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
   at FluentAssertions.Execution.CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context)
   at FluentAssertions.Collections.CollectionAssertions`2.ContainEquivalentOf[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs)
   at Root.CLIENTBooking.Service.Tests.AccountServices.AccountServiceTests.AddAccount_AddsAccountToContextWithGeneratedId() in C:\Users\XXXXXoz\OneDrive\Documents\XXXXX TEst\Root.CLIENTBooking.Service.Tests\AccountServices\AccountServiceTests.cs:line 110

测试

  [Test]
    public void AddAccount_AddsAccountToContextWithGeneratedId()
    
        //arrange
        var request = _fixture.Create<AddAccountRequest>();

        var expected = new Account
        
            FirstName = request.FirstName,
            LastName = request.LastName,
            DateOfBirth = request.DateOfBirth,
            Gender = (int)request.Gender,
            Email = request.Email,
            Orders = new List<Order>(),
            Created = DateTime.UtcNow
        ;

        //act
        _AccountService.AddAccount(request);

        //assert
        _context.Account.Should().ContainEquivalentOf(expected, options => options.Excluding(Account => Account.Id));
    

帐户类别

    public class Account

    public long Id  get; set; 
    public string FirstName  get; set; 
    public string LastName  get; set; 
    public DateTime DateOfBirth  get; set; 
    public int Gender  get; set; 
    public string Email  get; set; 
    public virtual ICollection<Order> Orders  get; set; 
    public DateTime Created  get; set; 

添加帐户类别

public class AddAccountRequest

    public string FirstName  get; set; 
    public string LastName  get; set; 
    public DateTime DateOfBirth  get; set; 
    public Gender Gender  get; set; 
    public string Email  get; set; 

终于

账户服务

  public void AddAccount(AddAccountRequest request)
    
        var validationResult = _validator.ValidateRequest(request);

        if (!validationResult.PassedValidation)
        
            throw new ArgumentException(validationResult.Errors.First());
        

        _context.Account.Add(new Account
        
            FirstName = request.FirstName,
            LastName = request.LastName,
            DateOfBirth = request.DateOfBirth,
            Gender = (int)request.Gender,
            Email = request.Email,      
            Orders = new List<Order>(),
            Created = DateTime.UtcNow
        );

        _context.SaveChanges();
    

【问题讨论】:

【参考方案1】:

我认为Created 时间不同,两个值设置在不同时间,因此不相等。

【讨论】:

以上是关于由于单元测试而面试失败-我需要了解原因[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

由于 otest 错误,无法从命令行运行 Xcode 单元测试

Dart 单元测试应因异常而失败

make / gcc:“错误构建”的可能原因是啥? [关闭]

单元测试 - Wiremock 验证失败并出现连接错误

具有多个模块和多个主类的spring boot项目-单元测试失败

串行执行单元测试(而不是并行)