简体   繁体   中英

How to Mock IMessageReceiver?

Currently i have scenario where i need to unit test a Service Bus Trigger Function. Fa code as below

public async Task Run([ServiceBusTrigger("sample", Connection = "sample", IsSessionsEnabled = false)] Message message, IMessageReceiver messageReceiver, ILogger _log)
        {
           
//Some code
        }  

I was primarily using MessageReceiver , but it's hard to unit test as it's not much flexible to Mock,so i switched to IMessageReceiver . Getting below Error

Microsoft.Azure.WebJobs.Host: Can't bind parameter 'messageReceiver' to type 'Microsoft.Azure.ServiceBus.Core.IMessageReceiver'.

NB:- It have a Weird issue that the variable name should be messageReceiver , while using MessageReceiver.

Is there anything that i need to follow for IMessageReceiver as well?

As of today, IMessageReceiver is not supported via dependency injection. You can only get MessageReceiver . You can upvote the request to add the support here .

Meanwhile, there's a workaround that you could use, showed here . The workaround is to have an additional, internal method, accepting IMessageReceiver and Function call that is injected MessageReceiver to pass the parameter to the internal method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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