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 8861901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:35:50+00:00 2026-06-14T15:35:50+00:00

Below is a class (Class1) that I want to test, but I’m not fully

  • 0

Below is a class (Class1) that I want to test, but I’m not fully satisfied with my Unit Test. Please see below code samples.

System Under Test

public interface IRepository {
    string GetParameter(int id);
}

public class Repository {
    public string GetParameter(int id) {
        return "foo";
    }
}

public class ErrorInfo {
    public string ErrorCodes { get; set; }
}

public interface IErrorProvider {
    ErrorInfo BuildErrorMessage(string errorCodes);
}

public class ErrorProvider {
    public ErrorInfo BuildErrorMessage(string errorCodes) {
        return new ErrorInfo(){ErrorCodes = errorCodes};
    }
}

public class Class1 {
    private readonly IRepository _repository;
    private readonly IErrorProvider _errorProvider;
    public Class1(IRepository repository, IErrorProvider errorProvider) {
        _repository = repository;
        _errorProvider = errorProvider;
    }

    public List<ErrorInfo> GetErrorList(int id) {
        var errorList = new List<ErrorInfo>();
        string paramName = _repository.GetParameter(id);

        if (string.IsNullOrEmpty(paramName)) {
            string errorCodes = string.Format("{0}, {1}", 200, 201);
            var error = _errorProvider.BuildErrorMessage(errorCodes);
            errorList.Add(error);
        }

        return errorList;
    }
}

Unit Tests

Below test passes and we check whether the correct error codes being used within the system under test.

[TestMethod]
public void GetErrorList_WhenParameterIsEmpty_ReturnsExpectedErrorCodes2() {
        //Arrange
        var stubRepo = new Mock<IRepository>();
        stubRepo.Setup(x => x.GetParameter(It.IsAny<int>())).Returns(string.Empty);

        var stubErrorMock = new Mock<IErrorProvider>();

        const int id = 5;
        var sut = new Class1(stubRepo.Object, stubErrorMock.Object);

        //Act
        var result = sut.GetErrorList(id);

        //Verify
        string verifiableErrorCodes = "200, 201";
        stubErrorMock.Verify(x => x.BuildErrorMessage(verifiableErrorCodes));
}

However I would prefer testing the end result. For example, I want to Assert against the error codes that have been used in the production code. Below test fails but I like to know your thoughts on how to Assert against the errorCodes that has been used in the system under test.

[TestMethod]
public void GetErrorList_WhenParameterIsEmpty_ReturnsExpectedErrorCodes1() {
        //Arrange
        var stubRepo = new Mock<IRepository>();
        stubRepo.Setup(x => x.GetParameter(It.IsAny<int>())).Returns(string.Empty);

        string expectedErrorCodes = "200, 201";
        var stubErrorRepo = new Mock<IErrorProvider>();
        stubErrorRepo.Setup(e => e.BuildErrorMessage(It.IsAny<string>()));

        const int id = 5;
        var sut = new Class1(stubRepo.Object, stubErrorRepo.Object);

        //Act
        var result = sut.GetErrorList(id);

        //Assert
        Assert.AreEqual(expectedErrorCodes, result.Single().ErrorCodes);
}

What would be the correct way to test this error codes that has been used in the system?

  • 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-06-14T15:35:51+00:00Added an answer on June 14, 2026 at 3:35 pm

    There is not really right or wrong answer and we have decided to use the Assert test as it test the end result.

    I took the TDD approach and re-implemented/analysed as below.

    • Start with a failing test (to simplify the code I removed the Repository from both test and the sut)

    //AssertTest

        [TestMethod]
        public void GetErrorList_WhenParameterIsEmpty_ReturnsExpectedErrorCodes1()
        {
            //Arrange
            const string expectedErrorCodes = "200, 201";
            var stubErrorRepo = new Mock<IErrorProvider>();
            stubErrorRepo.Setup(e => e.BuildErrorMessage(expectedErrorCodes)).Returns(new ErrorInfo() { ErrorCodes = expectedErrorCodes });
    
            var sut = new Class1(stubErrorRepo.Object);
    
            //Act
            var result = sut.GetErrorList();
    
            //Assert
            Assert.AreEqual(expectedErrorCodes, result.Single().ErrorCodes);
        }
    

    //SUT

        public IEnumerable<ErrorInfo> GetErrorList(int id)
        {           
            yield return new ErrorInfo();            
        }
    

    As you would expect the test fail.

    Now if write enough production code to make this test pass.

        public IEnumerable<ErrorInfo> GetErrorList()
        {
            yield return _errorProvider.BuildErrorMessage("200, 201");
        }
    

    The VerifyTest would still fail for the above SUT.

    //VerifyTest

       [TestMethod]
        public void GetErrorList_WhenParameterIsEmpty_ReturnsExpectedErrorCodes2()
        {
            //Arrange
            var stubErrorMock = new Mock<IErrorProvider>();
            var sut = new Class1(stubErrorMock.Object);
    
            //Act
            sut.GetErrorList();
    
            //Verify
            string verifiableErrorCodes = "200, 201";
            stubErrorMock.Verify(x => x.BuildErrorMessage(verifiableErrorCodes));
        }
    

    However if I want this test to pass, I can write the below production code as below

        public IEnumerable<ErrorInfo> GetErrorList()
        {
            _errorProvider.BuildErrorMessage("200, 201");
            return null;
        }
    

    Now the VerifyTest passes, but the AssertTest fails.

    Both tests are valid in their own ways. However they test different semantics.
    AssertTest test whether the end result contains the correct error codes. Verify test ensures
    the method is called with the correct error codes. It is important to note that
    the end value of the Assert test is based on the setup method “match” provided by the Moq
    framework. In other words the setup dictates what the end result would be.
    AssertTest would fail if the setup is configured incorrectly or the production code uses error codes that does not match the setup configuration.

    It is preferred to use the AssertTest as it test the end result.

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

Sidebar

Related Questions

I have the below class I want to unit test: public class MyClass {
below code is my databasehandler class i got it from a tutorial. Beside that
I'm struggling to convert the below code to C#. Class Class1 Implements IMyInterface Public
The test class below verifies that a simple HttpService gets content from a given
I have a class that implements InvocationHandler as below: public class MyProxyClass implements InvocationHandler
I have a class that I am using below. And I am using this
If I have a class that looks like below, how to I pull out
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have two classes Foo and Bar that Bar extends Foo as below: class
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class

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.