如何为ServiceBusReceivedMessage的模拟设置消息主体

huangapple go评论68阅读模式
英文:

how to set body for mock of ServiceBusReceivedMessage

问题

I am trying to create mock for ServiceBusReceivedMessage with the body. How to do it?

我正在尝试为ServiceBusReceivedMessage创建mock,并设置消息体。该如何做?

I have data variable which I want to set as message body, how to do it?

我有一个名为data的变量,我想将其设置为消息体,该如何操作?

var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventData));

var mockMessage = new Mock<ServiceBusReceivedMessage>();
var messageBody = new BinaryData(data);
mockMessage.Setup(x => x.Body).Returns(messageBody);
var message = mockMessage.Object;

以上是您提供的代码部分的翻译。

英文:

I am trying to create mock for ServiceBusReceivedMessage with the body. How to do it?

I have data variable which I want to set as message body, how to do it?

var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventData));

var message = new Mock&lt;ServiceBusReceivedMessage&gt;().Object;
        
//message.Body;

I tried below code, compile no error, but while running giving error:

> System.NotSupportedException : Unsupported expression: x => x.Body
Non-overridable members (here: ServiceBusReceivedMessage.get_Body) may not be used in setup / verification expressions.

var mockMessage = new Mock&lt;ServiceBusReceivedMessage&gt;();
var messageBody = new BinaryData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventData)));
mockMessage.Setup(x =&gt; x.Body).Returns(new BinaryData(messageBody));
var message = mockMessage.Object;

What's wrong here?

答案1

得分: 2

I understand your request. Here is the translated content:

我理解您的要求。以下是翻译好的部分:

从我的理解来看,您想要创建一个具有特定数据的类实例,我建议您使用Nuget存储中的AutoFixture

示例:

var fixture = new Fixture();
var messageBody = new BinaryData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventData)));
var mockedServiceBusReceivedMessage = fixture
    .Build<ServiceBusReceivedMessage>()
    .With(e => e.Body, messageBody)
    .Create();

如果您想要避免为其他属性生成自动数据,请在.Create()之前添加.OmitAutoProperties()

如果您想要在ServiceBusReceivedMessage中使用Moq,首先您应该为您的类创建一个接口:

public interface IServiceBusReceivedMessage
{
    BinaryData Body { get; }
}

然后将您的类替换为该接口:

public class ServiceBusReceivedMessage : IServiceBusReceivedMessage
{
    BinaryData Body { get; set; }
}

然后,您可以使用IServiceBusReceivedMessageBody设置模拟响应,就像您的示例中一样。但我怀疑您是否需要从BinaryData构造BinaryData,所以可能应该是:

var mockMessage = new Mock<IServiceBusReceivedMessage>();
var messageBody = new BinaryData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventData)));
mockMessage.Setup(x => x.Body).Returns(messageBody);
var message = mockMessage.Object;

也许我在这里说得很明显,但仍然会记录下来:
在所有需要ServiceBusReceivedMessage参数的地方,应该将其更改为IServiceBusReceivedMessage,以便接受这个新的模拟对象。

英文:

From what I understand, you want to create an instance of a class with specific data in it, for which I would recommend to use AutoFixture from Nuget store

Example:

var fixture = new Fixture();
var messageBody = new BinaryData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventData)));
var mockedServiceBusReceivedMessage = fixture
    .Build&lt;ServiceBusReceivedMessage&gt;()
    .With(e =&gt; e.Body, messageBody)
    .Create();

If you want to avoid generating automatic data for other properties, add .OmitAutoProperties() before .Create().

If you want to use Moq for ServiceBusReceivedMessage, first you should create an interface for your class:

public interface IServiceBusReceivedMessage
{
    BinaryData Body { get; }
}

And then substitute your class to this interface:

public class ServiceBusReceivedMessage : IServiceBusReceivedMessage
{
    BinaryData Body { get; set; }
}

Then you could use IServiceBusReceivedMessage to setup mocked response for Body as in your example, but I doubt that you need to construct BinaryData from BinaryData, so it would probably be:

var mockMessage = new Mock&lt;IServiceBusReceivedMessage&gt;();
var messageBody = new BinaryData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventData)));
mockMessage.Setup(x =&gt; x.Body).Returns(messageBody);
var message = mockMessage.Object;

And maybe I'm stating obvious here, but still will note this down:
Everywhere where there's ServiceBusReceivedMessage parameter, it should be changed to IServiceBusReceivedMessage for this new mocked object to be accepted.

huangapple
  • 本文由 发表于 2023年4月19日 23:27:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定