简体   繁体   English

单元测试Parallel.Foreach C#

[英]Unit Testing Parallel.Foreach C#

I have been using JustMock for unit testing in C#. 我一直在使用JustMock进行C#中的单元测试。 The problem I am facing is I cannot assert the functions called inside Parallel.Foreach . 我面临的问题是我无法断言Parallel.Foreach调用的函数。 However the assignments done inside can be asserted. 但是,可以断言内部完成的分配。

Parallel.ForEach(aList, entity =>
{
    //Can be asserted using  Assert(5,parameter.value) in the test
    parameter.value = 5;

    //Cannot be asserted, assertion fails Mock.Assert(parameter) in the test
    //is arranged using MustBeCalled
    parameter.call();   
})

I found the same issue in other test cases also. 我在其他测试用例中也发现了同样的问题。 Is this some wrong behavior of JustMock? 这是JustMock的一些错误行为吗?

When doing parallel processing you cannot easily assume that something will or will not happen, that's what makes it harder. 在进行并行处理时,您不能轻易地假设某些事情会发生或不会发生,这就更难以实现。 Instead, you concentrate on signs that will tell you that something is working. 相反,你专注于会告诉你某些事情正在发挥作用的迹象。 The issue here is that Parallel.ForEach stops processing as soon as exception is spotted, while you were expecting it to process all items. 这里的问题是,一旦发现异常, Parallel.ForEach停止处理,而您期望它处理所有项目。 Nothing is wrong with your JustMock . 你的JustMock没有任何问题。 Using normal foreach should resolve this issue. 使用普通的foreach应该可以解决这个问题。 if you have reason to use Parallel.Foreach anyway, try to catch all Exception fires here. 如果您有理由使用Parallel.Foreach ,请尝试在此处捕获所有Exception fires。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM