- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个单元测试,我试图验证我已经处理了曾经附加到主用户界面的文档。单元测试必须是异步的,因为所有内容都需要在 STA 线程下运行,我必须等待创建用户界面。
我有一个助手可以将操作分派(dispatch)到 STA 线程。
我在测试的主体中创建了 memory 对象,然后将其传递给异步方法,如下所示。
查看用 ### 注释的代码行以查看实际的问题行。 dotMemory 报告该对象尚不存在,但我已经断言证明该对象确实存在。
(STA Helper 类可以在 https://gist.github.com/bradphelan/cb4f484fbf6a7f9829de0dd52036fd63 找到)
这是与 async 和 dotMemory 相关的问题吗?
[Collection("Memory leaks")]
public class MemLeakSpec
{
public MemLeakSpec(ITestOutputHelper output)
{
DotMemoryUnitTestOutput.SetOutputMethod(output.WriteLine);
}
[Fact]
[DotMemoryUnit(FailIfRunWithoutSupport = true)]
public void ShouldCollectProjectX()
{
dotMemory.Check (memory => { STAThread.Run(() => ShouldCollectProjectAsyncX(memory)).Wait(); });
}
class Document { }
class Container { public Document Document; };
Document CreateFoo() => new Document();
private async Task ShouldCollectProjectAsyncX(Memory memory)
{
await Task.Delay(TimeSpan.FromMilliseconds(50));
Container container = new Container();
memory.GetObjects(@where => @where.Type.Is<Document>())
.ObjectsCount.Should()
.Be(0);
Document documentA = CreateFoo();
container.Document = documentA;
// Verify with dotMemory that the object exists.
// ### This fails even though I have verified
// ### the document exists
memory.GetObjects(@where => @where.Type.Is<Document>())
.ObjectsCount.Should()
.Be(1);
// Open a new project which should dispose the old one and thus
// remove any reference to GenericWeinCadFolder
container.Document = null;
memory.GetObjects(@where => @where.Type.Is<Document>())
.ObjectsCount.Should()
.Be(0);
GC.KeepAlive(container);
}
}
我已经创建了上述相同测试的同步版本并且它没有失败。下面有两个测试 ShouldCollectAsync 和 ShouldCollectSync。 async 失败,sync 通过。
[Collection("Memory leaks")]
public class MemLeakSpec
{
public MemLeakSpec(ITestOutputHelper output)
{
DotMemoryUnitTestOutput.SetOutputMethod(output.WriteLine);
}
[Fact]
[DotMemoryUnit(FailIfRunWithoutSupport = true)]
public void ShouldCollectAsync()
{
dotMemory.Check (memory => { STAThread.Run(() => ShouldCollectProjectAsyncX(memory)).Wait(); });
}
/// This test is almost identical to the ShouldCollectAsync
/// but it passes
[Fact]
[DotMemoryUnit(FailIfRunWithoutSupport = true)]
public void ShouldCollectSync ()
{
dotMemory.Check (memory => { STAThread.Run(() => ShouldCollectProjectSync(memory)); });
}
class Document { }
class Container { public Document Document; };
Document CreateFoo() => new Document();
private async Task ShouldCollectProjectSync(Memory memory)
{
Container container = new Container();
memory.GetObjects(@where => @where.Type.Is<Document>())
.ObjectsCount.Should()
.Be(0);
Document documentA = CreateFoo();
container.Document = documentA;
// Verify with dotMemory that the object exists.
// #### Passes here
memory.GetObjects(@where => @where.Type.Is<Document>())
.ObjectsCount.Should()
.Be(1);
GC.KeepAlive(documentA);
GC.KeepAlive(container);
}
private async Task ShouldCollectProjectAsyncX(Memory memory)
{
await Task.Delay(TimeSpan.FromMilliseconds(50));
Container container = new Container();
memory.GetObjects(@where => @where.Type.Is<Document>())
.ObjectsCount.Should()
.Be(0);
Document documentA = CreateFoo();
container.Document = documentA;
// Verify with dotMemory that the object exists.
// #### FAILS here
memory.GetObjects(@where => @where.Type.Is<Document>())
.ObjectsCount.Should()
.Be(1);
GC.KeepAlive(documentA);
GC.KeepAlive(container);
}
}
最佳答案
对我来说,将测试包装在本地函数中就足够了:
public void TestMethod()
{
async Task LocalFunction()
{
// here you can make async calls
await Task.Delay(...);
}
LocalFunction().ConfigureAwait(true).GetAwaiter().GetResult();
}
关于c# - 我在结合 dotMemory、xUnit 和 async 时做错了什么吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968613/
我找不到任何关于我认为是一个相当明显的问题的信息。xunit.console.clr4 命令行 arg 帮助状态: usage: xunit.console.clr4 [options] usage
当我们使用 MSTest 时,我们有几个环境有自己的运行设置。由于 Microsoft 正在放弃 MSTest,我们将转向 xUnit。无论是通过运行设置还是命令行属性,我都需要一种在 xUnit 测
我试图使用 OpenCover(今天下载)来覆盖我的测试。这是我使用的命令行: OpenCover.Console.exe -target:"c:\Programmes2\xunit\xunit.co
我正在学习使用单元测试,我创建了一个项目,添加了 xunit 引用。 以及以下代码: namespace UnitTestProject { public partial class Form
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
我有一个在 Windows 上构建的程序集 我想在 Linux 中的单声道上运行 xUnit 测试。 但是,我发现虽然其中 400 个测试可以(按顺序)运行,但某些测试要么挂起 xUnit 运行程序,
我有一个 F# 类库,其中包含使用 NuGet 安装的“xUnit.net”和“xUnit.net Runners”包。我有以下代码: module XUnitTest open Xunit [] l
I know that this question has been asked multiple times before I raise it again, However I still cou
我们使用的是 dotCover 2.7 和 xUnit.net 1.9.2。 在我的机器 (Windows 7) 和同事的机器 (Windows 8) 上,我们可以从命令行针对我们使用 xUnit.n
是否可以将 xUnit 与 LINQPad 一起使用? 能够首先为在 LINQPad 中设计的概念编写一些测试会很棒。这比添加另一个 ConsoleApp23423894238 更容易,只是为了能够快
我有一组需要共享状态的 xunit.net 测试。希望我希望这些测试能够并行运行。所以我希望运行者这样做: 创建共享夹具 运行并行所有使用该夹具的测试 在阅读 xunit 文档时,它说要在测试类之间共
我需要跳过基于类中某些 bool 条件的测试方法。是否可以?如何实现?我试过扩展 FactAttribute 但我无法获得 Test 类的实例。 我的代码如下: using System; using
我有兴趣在多个类上重用测试理论,特别是一些需要相同测试的构造函数。我最初有使用委托(delegate)来执行此功能的想法。 但是,我认为我可能正在尝试重新发明轮子,尽管 C# 具有一些功能,但我认为我
我正在为我的 DataAccessRepository(使用 Entity Framework )类编写测试用例。此类在构造函数中采用两个参数。 1)连接对象 2) Automapper 对象 现在,
xUnit 相当于 NUnit 的 [TestFixtureSetUp] ? 我们探索发现IUseFixture相当于[TestFixtureSetUp] ,但它没有按预期工作。 正如我们所探索的(在
Xunit 1.9.x 为用户提供了 DynamicSkipExample.cs 帮助他设置动态跳过 [Fact] 的示例。 事实证明,这在执行一些跨平台开发时非常有用。当由于底层上下文(操作系统、文
我正在尝试使用 xUnit.net 作为 SpecFlow 的测试运行器。来自官方下载区的 SpecFlow 1.2 二进制文件不包含 xUnit.net 提供程序,但 GitHub 上的主分支有一个
当我尝试运行我的 xUnit.net 测试时,出现此错误: [xUnit.net 00:00:00.63] xunit.UnitTest1.TestTheAnswer [FAIL] Fai
我已经转移到 xunit.net 2.0 测试版,但需要 AutoFixture 的一些功能,这仍然取决于当前的 1.9.2 稳定版本 (CompositeDataAttribute)。据我所知 Au
我已经使用 NUnit 多年了,我想尝试 XUnit。所以我安装了 XUnit 并运行了允许您通过 TD.net 运行 XUnit 的可执行文件。 我似乎不能一次运行多个测试。使用 NUnit + T
我是一名优秀的程序员,十分优秀!