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></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.
source share