Search code examples
visual-studiounit-testingmstestmsmqmicrosoft-fakes

How to fake a call to MSMQ.Send


I have the following test code:

 using (ShimsContext.Create())
            {                           

                // act
                sut.MethodCall();
            }

The SUT has the following method (for MethodCall):

  Dim mq As New MSMQ.MessageQueue(messageQPath)
  mq.Send(mqMsg)

But I'm getting following error:

  "The queue does not exist or you do not have sufficient permissions to perform the operation."

Obviously the queue won't exist and I won't have sufficient permissions if I don't have a queue created on the fake message queue. Has anyone got any experience with working with MSMQ and Fakes so that the call to the MSMQ send is basically a no operation which I can verify?


Solution

  • The shim needs to be set-up like so:

     ShimMessageQueue.AllInstances.SendObject = (m, o) =>
                    {
                        // verification code here
                    };
    

    As Fakes doesn't have concept of verifying a call directly using the framework, you just put the verification code inside the lambda for the SendObject call.