9

I have a interface which has a method to create instance of queue client and message factory. I have created a concrete implementation of the same and now i am trying to unit test the functionality which is used for send message to queue. I have written below code for the same:

private readonly Mock<IQueueClientProvider> queueClientProvider;

private readonly Mock<IJsonSerializer> jsonSerializer;

private readonly Mock<ILogger<QueueConnector>> logger;

private readonly ModelLogger<QueueConnector> modelLogger;

private readonly QueueConnector queueConnector;

private readonly Mock<QueueClient> queueClient;

public QueueConnectorTests() 

{

   queueClientProvider = new Mock< IQueueClientProvider >();

   queueClient = new Mock< QueueClient >();

   logger = new Mock< ILogger< QueueConnector >>();

   jsonSerializer = new Mock< IJsonSerializer >();

   modelLogger = new ModelLogger<QueueConnector>(logger.Object,     jsonSerializer.Object);

queueConnector = new QueueConnector(modelLogger, queueClientProvider.Object);

}

[Fact]
public async void ShouldBeAbleToSendMessage()

{

  BrokeredMessage brokeredMessage = new BrokeredMessage();

brokeredMessage.CorrelationId = "0HKVUFCD6Q5JL";

queueClientProvider.Setup(q =>q.CreateQueueClient()).Returns(queueClient.Object);

// Act

await queueConnector.SendMessage(brokeredMessage);

// Assert

 queueClient.Verify(q => q.SendAsync(brokeredMessage));


}

but getting exception

An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code

Additional information: Parent does not have a default constructor. The default constructor must be explicitly defined.

Can someone please help me with this not sure whats going wrong here?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Sudama Tripathi
  • 349
  • 1
  • 5
  • 20
  • Possible duplicate of [Mocking a type with an internal constructor using Moq](http://stackoverflow.com/questions/3279412/mocking-a-type-with-an-internal-constructor-using-moq) – Nkosi Nov 10 '16 at 11:18
  • we would need more details: which line is throwing the exception ? Also, could you post the code of QueueConnector's constructor and SendMessage method ? – Fabio Salvalai Nov 12 '16 at 17:25
  • How did you solve this issue? – Jeeten Parmar Jul 24 '18 at 13:49
  • I had to restructure my code, sendAsync method was expecting a brokered message which could not be mocked so I created my own implement ion of the same if I remember correctly. It's been a while when I resolved this issue. – Sudama Tripathi Oct 10 '18 at 02:47

1 Answers1

21

You should use the IQueueClient interface inside your code instead of the QueueClient implementation. The SendAsync method is part of the ISenderClient interface which IQueueClient inherits from.

Once you've used IQueueClient, you can do something like this:

var queueClientMock = new Mock<IQueueClient>();
queueClientMock.Setup(x => 
x.SendAsync(It.IsAny<Message>))).Returns(Task.CompletedTask).Verifiable();
Dibran
  • 1,435
  • 16
  • 24