延迟NUnit的断言消息评估 [英] Delayed NUnit Assert message evaluation
问题描述
我有这样的断言在我的测试代码
I have this assert in my test code
Assert.That(() => eventData.Count == 0,
Is.True.After(notificationPollingDelay),
"Received unexpected event with last event data" + eventData.Last().Description());
这一段时间之后,并且在失败时产生一个消息断言一些条件。它无法运行,因为当断言结束消息字符串断言启动时,而不是将。因此, EVENTDATA
收集仍然是空的(因为它最初是),并试图获得说明
的最后一个项目集合中的失败。有一种解决方法或体面的替代这 NUnit的
或我必须恢复使用 Thread.sleep代码
在我测试
that asserts some condition after a period of time and on failure produces a message. it fails to run because the message string is constructed when the assert starts and not when the assert ends. therefore the eventData
collection is still empty (as it is initially) and the attempt to get the Description
of the last item in the collection fails. is there a workaround or decent alternative to this in NUnit
or do I have to revert to using Thread.Sleep
in my tests?
PS:我使用NUnit的2.5.10
PS: I'm using NUnit 2.5.10.
推荐答案
您可以使用此方案:
var constrain = Is.True.After(notificationPollingDelay);
var condition = constrain.Matches(() => eventData.Count == 0);
Assert.IsTrue(condition,
"Received unexpected event with last event data" +
eventData.Last().Description());
这方法类似于使用Thread.sleep代码
This method is similar to the use Thread.Sleep
这篇关于延迟NUnit的断言消息评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!