How does this Moq test work mainly regarding linq syntax?

I watched this Introduction to Moq Videos on Dimecasts.net. In the video, when a guy installs 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 do all string types testing the SendBatchEmails method? I guess I'm a bit confused about how mocking works.

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

3) At first the author had "," "in SendEmail function, but it failed, so he did it instead    It.IsAny<string>

, but I still don't understand why it failed with", "".

Stackoverflow doesn't put the string keyword in angle brackets. (Fixed)

+2


source to share


3 answers


1) Moq does not affect how it works SendBatchEmails

, since you are not mocking the class Emailer

. Instead, you are mocking the interface IEmailService

, so if the class Emailer

calls methods on the object IEmailService

, Moq will record those calls.

2) You tell Moq that you are expecting a method call SendEmail

on IEmailService

. You also tell Moq that when SendEmail

called, you want Moq to automatically return true

.



Since the Moq object is passed to the class Emailer

, this test will be checked if the code in the SendBatchEmails

method calls SendEmail

.

3) Waiting is realized only if the parameters match. If you tell Moq you expect to SendEmail

be called with "", ""

as parameters, but SendEmail

called with different strings, it will fail. It.IsAny<string>()

indicates that Moq matches the expectation with any string as a parameter.

+4


source


It does not iterate over any lines, and the wait to be set will simply match whatever line it sets when calling SendEmail, returning a true result. The lambda inside the wait sets the function call match on the object that the wait is set on, in this case mockEmailService. The variable x here takes the value of the object that is the waiting object. The method that this object will match is SendEmail, and in addition, it will match when SendEmail is called with any string parameters. When the expected call is expected, it will return true.



Presumably the reason it failed initially is because SendEmail is being called with a couple of parameters that don't match "," ". This meant the wait was not fulfilled, and therefore it did not return. that expectations were not met, the test probably failed, although it could also be that SendBatchEmail failed when the method returned false.

+1


source


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

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

3) I will answer this first (2). The reason "," "failed because it should have logged the expected call to mockEmailService.SendMail (", ""). If called with any other parameters, expectations will not be triggered. Using IsAny () means that you don't need the parameter values, you just expect the method call to happen.

And finally (2) - the syntax registers the wait with Moq. The Expect () method simply requests a method to be called - in this case mockEmailInstance.SendMail () with any parameters.

+1


source







All Articles