首页 > 解决方案 > 单元测试中的堆栈溢出错误(Resharper、C#、NUnit)。测试中止

问题描述

我尝试在项目中运行我的单元测试,但它中止了堆栈溢出异常的原因。

似乎有很多电话,但我不明白是什么问题以及如何解决它。可能是关于我如何设置测试的问题

这是我的测试设置和配置代码。如您所见,我调用了_dataQuery.Execute(TestTime)方法,之后会话被中止。这种方法在主项目中效果很好

希望得到您的帮助!

      internal abstract class PermissionsTests
      {
           protected static DateTime TestTime;
           protected ILifetimeScope Scope;
           private MyDbContext _dbContext;

           [OneTimeSetUp]
           public void Setup()
           {

            var options = new DbContextOptionsBuilder<MyDbContext>()
                .UseInMemoryDatabase(databaseName: "PermissionsTests")
                .EnableSensitiveDataLogging()
                .Options;
            _dbContext = new MyDbContext(options);

            Scope = ConfigureContainer()
                .BeginLifetimeScope(b =>
                    b.RegisterInstance(new FakeUserQuery(_dbContext)).AsImplementedInterfaces());

            if (!_dbContext.Database.EnsureCreated()) return;
            TestTime = DateTime.UtcNow;

            //seed data to database
           }
      }

     internal abstract class DataPermissionsTests : PermissionsTests
    {
        [OneTimeSetUp]
        public void SetupCustom()
        {
            _dataQuery = Scope.Resolve<IDataQuery>();

            _login = new LoginAsAttribute(userId);
            _login.Login();

            Userdata = _dataQuery.Execute(TestTime);          
             //exception are inside this method. includes some queries
             //needed to get data from database for test user       

            _login.Logout();       
        }
    }

UPD

我遵循了建议并调试了测试中的每一个最小步骤,我发现了异常明显的方法(当这个查询执行时它会下降):

var serviceDocuments = dbContext.ServiceDocumentRelations
                .AsNoTracking()
                .Where(x =>
                    x.ServiceInfo.Updated.At >= criteria ||
                    x.Document.ServiceInfo.Updated.At >= criteria ||
                    x.Biography.ServiceInfo.Updated.At >= criteria ||
                    x.Event.ServiceInfo.Updated.At >= criteria ||
                    x.Event.Participants.AsQueryable().Any(p => 
                           p.ServiceInfo.Updated.At >= criteria) ||
                    x.Company.ServiceInfo.Updated.At >= criteria ||
                    x.News.ServiceInfo.Updated.At >= criteria ||
                    x.Media.ServiceInfo.Updated.At >= criteria ||
                    x.Note.ServiceInfo.Updated.At >= criteria ||
                    x.Assignment.ServiceInfo.Updated.At >= criteria ||
                    x.AssignmentPerformer.ServiceInfo.Updated.At >= criteria ||
                    x.PhotoUserSettings.ServiceInfo.Updated.At >= criteria ||
                    x.BackgroundUserSettings.ServiceInfo.Updated.At >= criteria
                );

所以我按条件划分了这个查询,然后我的问题就解决了。但我不明白为什么

谢谢你

标签: c#unit-testingnunitresharperstack-overflow

解决方案


推荐阅读