我有一个集成测试,它总是失败(在Visual Studio中),但是,通过查看数据库来验证结果表明这里测试的系统实际上是成功的。
这是测试的基本思想:
private static readonly EfContext db = new EfContext();
[TestMethod]
void Complete_System_Run_Through_Is_Successful()
{
// Create a new unique message and request...
var message = Guid.NewGuid().ToString();
var request = new FooRequest { Message = message };
var fooClient = null; /* WCF proxy */
try
{
// Call the service...
fooClient = new FooClient();
fooClient.CallService(fooRequest);
}
finally
{
// Close client or Abort faulted client...
var channel = fooClient as ICommunicationObject;
try
{
if (channel.State != CommunicationState.Faulted)
channel.Close();
}
catch { channel.Abort(); }
}
// Verify there are 15 instances (traces) present in the database...
var actualNumberOfTraces = db.Traces.Count(x => x.Message == message);
Assert.AreEqual(15, actualNumberOfTraces);
}正在测试的WCF服务触发了一系列其他下游服务(比如“服务总线”),其中每个侦听服务都会向数据库添加一个条目(跟踪)。从头到尾,这个过程记录了每个完整系统运行的15个这样的跟踪。
验证数据库中的结果表明测试运行成功(数据库中存在所有15个跟踪)。然而,测试运行失败(在Visual Studio中),发现的实际跟踪数在3-6之间。我能想到的唯一一件事是,Assert调用得太早了(即数据库还没有完成更新)。
无论如何,一切都在正常工作,所有的痕迹都确实存在于数据库中,我只是在这个测试中遇到了问题。有什么建议吗?
发布于 2012-05-29 23:44:07
看起来你有一个异步方法调用。本文描述WCF同步和异步行为。
How to Call WCF Services Synchronously and Asynchronously
发布于 2012-05-30 03:30:41
好吧,目前最糟糕的解决方案是添加一个Thread.Sleep(250),让数据库有时间跟上。如果你能想出一个更优雅的解决方案,我绝对有兴趣听听!
https://stackoverflow.com/questions/10800450
复制相似问题