Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:27:36+00:00 2026-05-18T04:27:36+00:00

I have a method in a service which I want to test. That method

  • 0

I have a method in a service which I want to test. That method calls another method in the same class. This method is already tested so I want to mock that 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 am mocking: BuildNewsLetterHTML returns null instead of the content it supposed to return.

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 doesn’t return the content string it supposed to return. The test fails on Assert.AreEqual because the actual is null. Any of you have any idea why actual is null?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:27:36+00:00Added an answer on May 18, 2026 at 4:27 am

    It seems the problem is you are calling Mock<T>'s CreateNewsLetter method which has not been set up, and which also seems to be your method under test. You are not supposed test against your fakes, they are supposed to substitute out production code to assist in testing other code.

    I suggest you use the extract and override pattern in this case, since you are wanting to cause fake implementation in a method of the same class that has a method under test.

    Moq is great in some cases but I don’t think there is anything wrong with using a small readable stub when the situation calls for 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 virtual.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I want to create a WCF service that have login method, which is
I have a service method that calls a DAO which then returns an object
Hello I have a simple wcf service like this, with a test method which
I want to test a Grails controller which calls a service. I'd like to
I have a simple web service and I want to make a method which
I have a method in my web service which returns a DataView, I have
I have a simple web service method which I'm trying to convert: <WebMethod()> _
I have a web service method in which I create a particular type of
Let's say I have a WCF service which has a method returning object Person.
When I call, the service's method, I have Web Service Exception happening which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.