tags:

views:

121

answers:

3

I watched this Introduction to Moq video on Dimecasts.net. In the video, when the guy sets up his moq test, he has the following code:

[Test]
public void TestWithMock()
{
    var mockEmailService = new Mock<IEmailService>();

    mockEmailService.Expect(x =>  
    x.SendEmail(It.IsAny<string>,It.IsAny<string>)).Returns(true);

    var emailer = new Emailer(mockEmailService.Object);

    emailer.SendBatchEmails();
}

Here are my questions:

1) Does moq loop through all different types of strings testing the SendBatchEmails method? I guess I am a little confused on how mocking works.

2) Can someone explain the lambda syntax of the Expect part?

3) The author first had "","" in the SendEmail function, but that failed, so he instead put It.IsAny<string>, but I am still unclear on why it failed with "","".

Stackoverflow is not putting the string keyword in the angle brackets. (Fixed)

+1  A: 

It doesn't loop through any strings, the expectation that is set up will simply match any string that it is given when SendEmail is called, returning a true result. The lambda inside the expectation sets up a function call match on the object upon which the expectation is set, in this case the mockEmailService. The variable x here takes on the value of the object that is the subject of the expectation. The method that it will match on that object is SendEmail, and further it will match when SendEmail is called with any string parameters. When the expected call occurs, it will return the value true.

Presumably the reason that it failed initially, is that SendEmail was called with a pair of parameters that didn't match "","". This meant that the expectation wasn't met and thus it didn't return true. The fact that the expectation wasn't met probably failed the test, though it could also have been that SendBatchEmail failed when the method returned false.

tvanfosson
+1  A: 

1) What do you mean, different types of strings? There's only one type of string... which is string.

What mocking does is automatically create a new object type that implements the interface or abstract class provided. In this case, Moq will generate a new class type, with a name similar to something like "IEmailService_a324bc54ff123827d". This type will have all the methods of the IEmailService interface implemented to return null.

3) I'll answer this first before (2). The reason "", "" failed, is because it would have been registering an expected call to mockEmailService.SendMail("", ""). If it got called with any other parameters, then the expectation would fail. Using IsAny() means that you don't care about the parameter values, you're just expecting the method call to happen.

And finally, (2) - the syntax is registering an expectation with Moq. The Expect() method just asks for the method that will be called - so in this case, mockEmailInstance.SendMail(), with any parameters.

womp
What I meant was "abc", "123" or "623","bcc". I guess types of strings wasn't the best word to use.
Xaisoft
+2  A: 

1) Moq doesn't affect how SendBatchEmails works, since you are not mocking the Emailer class. Instead you're mocking the IEmailService interface, so if the Emailer class calls methods on the IEmailService object, Moq will capture those calls.

2) You are telling Moq that you're expecting the SendMail method on IEmailService to be called. You're also telling Moq that when SendMail is called, you want Moq to automatically return 'true'.

Since the Moq object is being passed into Emailer class, this test would be testing if the code in SendBatchEmails calls the SendMail method.

3) An expectation is realized only if the parameters match. If you tell Moq you expect SendMail to be called with "","" as the parameters, and SendMail is called with different strings, it will fail. It.IsAny<string>() tells Moq to match the expectation with any string as the parameter.

Tinister
+1. Note also that this doesn't really test anything - there are no asserts, nor is the expectation Verifiable, nor is it calling mockEmailService.Verify(). (I usually do state testing, but behavior testing may be warranted here.)
TrueWill
Thanks for the explanation.
Xaisoft