The meaning of the method in the same interface with MOQ

I have a method in a service that I want to test. This method calls another method in the same class. This method has already been tested, so I want to mock this method.

This is my setup:

private readonly Mock<INewsLetterRepository> _mockNewsLetterRepository; private readonly Mock<INewsLetterService> _mockNewsLetterService; private readonly INewsLetterService _newsLetterService; public NewsLetterServiceTest() { _mockNewsLetterRepository = new Mock<INewsLetterRepository>(); _mockNewsLetterService = new Mock<INewsLetterService> {CallBase = true}; _newsLetterService = new NewsLetterService(_mockNewsLetterRepository.Object); } 

And this is the test I am using:

 [TestMethod] public void CreateNewsLetter_Should_Return_Empty_NewsLetter() { var template = new Template { PlaceHolders = new List<TemplatePlaceholder>() }; var newsLetter = new NewsLetter {Template = template}; const string content = "<html><body><!--BROWSER--></body></html>"; _mockNewsLetterService.Setup(x => x.BuildNewsLetterHTML(It.IsAny<NewsLetter>())).Returns(content); var actual = _mockNewsLetterService.Object.CreateNewsLetter(newsLetter); Assert.AreEqual(content, actual); } 

Now the problem is that the function I'm mocking: BuildNewsLetterHTML returns null instead of the content that it should have returned.

Here is a simplified version of the function I want to test:

 public string CreateNewsLetter(NewsLetter newsLetter) { var newsletterHTML = BuildNewsLetterHTML(newsLetter); return newsletterHTML; } 

So the problem is that at least, as I see it, is that the function i mock does not return the content string that it should return. The test fails on Assert.AreEqual because the actual value is null. Do any of you have an idea why the actual value is null?

Thanks in advance.

+4
source share
1 answer

The problem seems to be that you are calling the Mock<T>'s CreateNewsLetter method, which has not been configured, and which also seems to be your test method. You do not expect a check against your fakes, they should replace the production code to help in testing other code.

I suggest you use extract and redefine the template in this case, since you want to call a false implementation in a method of the same class that the test method has.

Moq is great in some cases, but I don’t think there is something wrong with using a small readable stub when the situation requires it.

 public class YourTestClass { [TestMethod] public void CreateNewsLetter_Should_Return_Empty_NewsLetter() { var template = new Template { PlaceHolders = new List<TemplatePlaceholder>() }; var newsLetter = new NewsLetter { Template = template }; const string content = "<html><body><!--BROWSER--></body></html>"; INewsletterService service = new BuildNewsLetterStub(content); string actual = service.CreateNewsLetter(newsLetter); Assert.AreEqual(content, actual); } } public class BuildNewsLetterStub : NewsLetterService { private string _letter; public BuildNewsLetterStub(string letter) { _letter = letter; } public override string BuildNewsLetterHTML(NewsLetter newsLetter) { return _letter; } } 

To override BuildNewsLetterHTML, it must be marked as virtual.

+6
source

All Articles