c# - 单元测试中的堆栈溢出错误(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
);
所以我按条件划分了这个查询,然后我的问题就解决了。但我不明白为什么
谢谢你
解决方案
推荐阅读
- mysql - 加入表以从其他表中获取 ID 名称
- react-native - 无法在 webview 中显示 html
- selenium - Selenium 使用 Java:使用类属性搜索按钮失败;而使用title属性是成功的
- javascript - 在js中使用if语句向对象添加字段
- wso2 - 不允许 WSO2 EI 6.1.1 DOCTYPE
- react-scripts - 我怎样才能让`react-scripts build`安静?
- c# - 用字典连接类
- ios - 在 Eureka 中使用 AlertRow 如何在触摸外部任何位置时关闭警报窗口
- jsf - 将元素添加到数据列表
- php - 在 PHP 中使用 curl 时无法得到任何响应