我正在使用RavenDB处理WebAPI应用程序。 我有几个XUnit测试有类似的大纲:
var checkQuery = session.Query<Resource>().Where(x => x.AliasIds.Any(a => a == alias.Id));
PAssert.Throws<InvalidOperationException>(() => checkQuery.Single());
var testString = Guid.NewGuid().ToString();
Controller.Post(testString);
var res = checkQuery.Single();
PAssert.IsTrue(() => res != null);
当我在同一时间运行多个测试时,他们会在
行失败var res = checkQuery.Single();
例外:
结果消息:System.InvalidOperationException:序列不包含任何元素
我发现了什么:
我尝试添加
store.DatabaseCommands.DisableAllCaching();
store.Conventions.ShouldCacheRequest = _ => false;
但它没有帮助。
答案 0 :(得分:2)
假设Controller.Post(testString)
正在添加新条目,您可能只有一个陈旧的索引。在现实世界中,一些自然的时间量将在帖子和查询之间传递。在单元测试中,您没有这种延迟,因此在索引上提供以下内容是很常见的:
.Customize(x => x.WaitForNonStaleResults())
这不是你应该在制作中做的事情。您可以阅读更多in the documentation here。