IEnumerable 和收益回报

发布于 2024-08-18 08:01:28 字数 997 浏览 1 评论 0原文

当我调用通用基类型的方法时,我的单元测试做了一些非常奇怪的事情。我尝试过 NUnit 和 MSTest,结果相同。这就是代码的组织方式:

public class MyStub {}

public class EnumerableGenerator
{
     public bool GotMyStubs;

     public IEnumerable<MyStub> GetMyStubs()
     {
         GotMyStubs = true;
         yield return new MyStub();
     }
}
public class ConsoleRunner
{
public void Main(string args[])
{
     EnumerableGenerator gen = new EnumerableGenerator();
     gen.GotMyStubs = false;
     var myStubs = gen.GetMyStubs();
     if  (!gen.GotMyStubs)
         return 1;
} 
}

测试显然失败了。奇怪的是,当我进行集成测试时,代码运行良好。它仅在单元测试中中断。我根本无法进入 GetMyStubs。方法内的断点也不会中断。我已经打开了对 CLR 抛出的所有异常的中断,但这也不会产生任何结果。

我检查了 GetMyStubs 返回的类型,该类型的全名如下所示:

[MyNamespace.EnumerableGenerator2+d__8[[MyNamespace.MyStub, MyNamespace, Version=7.1.0.0, Culture=neutral, PublicKeyToken=null]]

现在,当我查看代码覆盖率数字(通过 MSTest)时,我注意到有一个 EnumerableGenerator.d__8 条目。

有人见过这样的东西吗?我完全迷失了......

My unit tests are doing something very strange when I call a method of a generic base type. I have tried both NUnit and MSTest with the same result. This is how the code is organized:

public class MyStub {}

public class EnumerableGenerator
{
     public bool GotMyStubs;

     public IEnumerable<MyStub> GetMyStubs()
     {
         GotMyStubs = true;
         yield return new MyStub();
     }
}
public class ConsoleRunner
{
public void Main(string args[])
{
     EnumerableGenerator gen = new EnumerableGenerator();
     gen.GotMyStubs = false;
     var myStubs = gen.GetMyStubs();
     if  (!gen.GotMyStubs)
         return 1;
} 
}

The test fails obviously. The strange part is that the code functions fine when I'm integration testing it. It only breaks in the unit test. I cannot step into GetMyStubs at all. Break points within the method do not break either. I have turned on breaking for all exceptions being thrown by the CLR and this yields nothing as well.

I examined the type returned by GetMyStubs and the type's full name looks like this:

[MyNamespace.EnumerableGenerator2+<GetMyStubs>d__8[[MyNamespace.MyStub, MyNamespace, Version=7.1.0.0, Culture=neutral, PublicKeyToken=null]]

Now when I look at my code coverage numbers (through MSTest) I notice that there is a EnumerableGenerator.d__8 entry.

Has anyone seen something like this? I'm completely lost....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-08-25 08:01:28

您给出的代码甚至无法编译 - IEnumerable 上没有 Count 属性。有一个 Count() 扩展方法 - 这是你的意思吗?

我强烈怀疑 MyMethod() 看起来也不是那样的。我怀疑它实际上使用 yield return 来返回项目...此时类型的全名就有意义了,因为它是由迭代器块为您生成的状态机。

假设是这种情况,当您调用 MyMethod() 时,将不会执行任何代码。它将创建状态机并返回它。当您开始迭代它时,然后它将开始执行您的代码...而这就是我预计会遇到断点的时候。

您能否提供一个简短但完整的失败示例(如果需要,使用 NUnit,但控制台应用程序将是理想的)?

The code you've given won't even compile - there isn't a Count property on IEnumerable<T>. There's a Count() extension method - is that what you meant?

I strongly suspect that MyMethod() doesn't really look like that either. I suspect it actually uses yield return to return the items... at which point the type's full name would make sense, as it's a state machine generated for you by the iterator block.

Assuming that's the case, when you call MyMethod(), that won't execute any of your code. It will create the state machine and return it. When you start iterating over it, then it will start executing your code... and that's when I'd expect break points to be hit.

Could you provide a short but complete example (using NUnit if necessary, but a console app would be ideal) of it failing?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文