chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

Does anyone have any advice for mocking MSMQ and MSMQ transactions? I am wondering if I should attempt to mock the constructors.

Jason
asked by jsouthgate (600 points)

4 Answers

0 votes
Please give an example so that we can help you.
I am sure that there are some times where you would mock the constructors and some time that you won't
answered by scott (32k points)
0 votes
Here is the code snippets that I need to mock all System.Messaging namespace members used here.

{
QueuePath = @".private$channelName";
MessageQueue Queue;
Message message = new Message("test message");

if (MessageQueue.Exists(QueuePath))
            {                
                Queue = new MessageQueue(QueuePath);
            }        
if (Queue != null)
            {                
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    Queue.Send(message, MessageQueueTransactionType.Automatic);                                        
                    scope.Complete();                    
                }
            }
}


Any special comment on mocking MSMQ and MSMQ Transaction this?
answered by tarangnpatel (650 points)
0 votes
Hi,

Basically you can do that. We're preparing an example. We'll post it here soon.

Thanks
answered by gilz (14.5k points)
0 votes
Tarang,

The following code uses the AAA API to set the behavior of the code you posted (I put it into MessagingTestClass.MessagingFunc()) to make it reach the Send() call, and verifies that call is actually made:

[TestMethod]
public void Test()
{
    // make the next instantiations of MessageQueue and TransactionScope return fake objects
    var fakeQueue = Isolate.Fake.Instance<MessageQueue>();
    Isolate.Swap.NextInstance<MessageQueue>().With(fakeQueue);

    var fakeTransaction = Isolate.Fake.Instance<TransactionScope>();
    Isolate.Swap.NextInstance<TransactionScope>().With(fakeTransaction);

    // make MessageQueue.Exists return true when queried
    Isolate.WhenCalled(() => MessageQueue.Exists("")).WillReturn(true);

    // call the method under test
    MessagingTestClass.MessagingFunc();

    // verify the Send() call was made
    Isolate.Verify.WasCalledWithAnyArguments(() => fakeQueue.Send(null, MessageQueueTransactionType.Automatic));
        }
    }


Please let me know if this helps.
Doron,
Typemock support
answered by doron (17.2k points)
...