Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在单元测试中尝试为Microsoft.ServiceBus.Messaging.QueueClient使用垫片时出错_C#_Unit Testing_Servicebus_Shim - Fatal编程技术网

C# 在单元测试中尝试为Microsoft.ServiceBus.Messaging.QueueClient使用垫片时出错

C# 在单元测试中尝试为Microsoft.ServiceBus.Messaging.QueueClient使用垫片时出错,c#,unit-testing,servicebus,shim,C#,Unit Testing,Servicebus,Shim,我使用QueueClient通过Azure服务总线发送消息,代码基于msdn上的一些示例: 还有一个链接,由于我的声誉,我无法在这里显示,但可以通过https在code.msdn.microsoft.com/windowsazure/Brokered-Messaging-Session-41c43fb4/sourcecode?fileId=124692&pathId=317475279上找到 实际上,我有QueueClient的工作代码,但我想做一些单元测试。问题就从这里开始。我在填充Mi

我使用QueueClient通过Azure服务总线发送消息,代码基于msdn上的一些示例:

  • 还有一个链接,由于我的声誉,我无法在这里显示,但可以通过https在code.msdn.microsoft.com/windowsazure/Brokered-Messaging-Session-41c43fb4/sourcecode?fileId=124692&pathId=317475279上找到
实际上,我有QueueClient的工作代码,但我想做一些单元测试。问题就从这里开始。我在填充Microsoft类时遇到问题

我的代码将接受QueueClient并使用它发送消息。我为该代码创建单元测试的第一步是尝试创建并测试QueueClient的一个垫片。我使用Send(BrokeredMessage)方法的迂回,在垫片上调用该方法,并断言调用了我的迂回

其中涉及到一些麻烦-QueueClient是抽象的,不实现任何接口,我必须使用带有假路径的create方法创建一个实例来填充。我不知道运行测试时这是否会导致问题

我最后做了以下测试:

using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.ServiceBus.Messaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;

... [some context removed]

        [TestMethod]
    public void TestFireAndForgetHappyPath2()
    {
        using (ShimsContext.Create())
        {
            const string messageBody = "Hi";
            string capturedBody = null;

            var shimmedClient = QueueClient.CreateFromConnectionString("Endpoint=sb://fake.net/;", "queueName");

            var shim = new Microsoft.ServiceBus.Messaging.Fakes.ShimQueueClient(shimmedClient)
            {
                SendBrokeredMessage = message => { capturedBody = message.GetBody<string>(); }
            };

            shim.SendBrokeredMessage(new BrokeredMessage(messageBody));
            Assert.AreEqual(messageBody, capturedBody);
        }
    }
使用Microsoft.QualityTools.Testing.Fakes;
使用Microsoft.ServiceBus.Messaging;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
... [删除某些上下文]
[测试方法]
public void testfire和forgethappypath2()
{
使用(ShimsContext.Create())
{
常量字符串messageBody=“Hi”;
字符串capturedBody=null;
var-shimmedClient=QueueClient.CreateFromConnectionString(“Endpoint=sb://fake.net/;”,“queueName”);
var shim=新的Microsoft.ServiceBus.Messaging.Fakes.ShimQueueClient(shimmedClient)
{
SendBrokeredMessage=message=>{capturedBody=message.GetBody();}
};
shim.SendBrokeredMessage(新的BrokeredMessage(messageBody));
AreEqual(messageBody,capturedBody);
}
}
我在VisualStudio中创建了代码。它不会在编辑器中抱怨(即没有红色曲线),但当我尝试构建时,会出现以下错误:

属性或索引器 “Microsoft.ServiceBus.Messaging.Fakes.ShimQueueClient.SendBrokeredMessage”无法在此上下文中使用,因为它缺少get访问器

我在微软的Shim文档中寻找了一些例子。他们甚至有一个与服务总线相关的示例,但他们不使用QueueClient。相反,他们使用了我在MSDN中找不到的类和接口(请参阅及其对IMicrosoftAzureQueue的使用)

有人能提供一些关于如何正确地对QueueClient进行存根/模拟/填充的示例吗


谢谢

我认为这是导致错误的原因:
shim.SendBrokeredMessage(newbrokeredmessage(messageBody))。你不应该直接给垫片打电话。测试应该调用调用
QueueClient.Send的代码。我现在没有时间测试它并确认它是答案,但它是有意义的,而且一旦被指出就很明显。我会尽快测试的。我想既然你发表了评论,你就不想因为正确的答案而获得所有的荣誉了吧?谢谢,这就解决了。为了完整性起见,上面的测试失败了一次,主机查找失败(我没有得到详细信息),因此这不是一个正确的确定性测试。但这是另一个问题。我发表评论是因为这更像是一个注释,而不是一个明确的答案,而且您可能需要有服务总线经验的人提供完整的答案。如果评论有帮助的话,你可以投票支持。