Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nunit -Check method was called with a parameter

Tags:

c#

nunit

The test should test the AddDebug method works correctly. So If the username is null It will automatically set the user name to "Anononymus". However I can't see the List because it's private, therefore the only way to check to function works correctly is take advantage of that opportunity that the AddDebug method calling the Add method. I have to verify that the Add method was called with the "Anonymous" parameter I only can use Nunit, Mock is not optional now for me.

private List<Log> Entries;
    public Logger()
    {
        Entries = new List<Log>();
    }
    public void Add(string text, LogLevels level, DateTime timeStamp, string userName)
    {
        var Enitity = new Log();
        Enitity.Text = text;
        Enitity.Level = level;
        Enitity.TimeStamp = timeStamp;
        Enitity.UserName = userName;
        Add(Enitity);

    }
    public void Add(Log log)
    {
        if (!(log.TimeStamp > DateTime.Now))
        {
            Entries.Add(log);
        }
    }
    public void AddDebug(string text, string userName = null)
    {
        if (String.IsNullOrEmpty(userName) == true)
        {
            Add(text, LogLevels.Debug, DateTime.Now, "Anonymus");
        }
        else
        {
            Add(text, LogLevels.Debug, DateTime.Now, userName);
        }

    }
like image 396
Qwadrox Avatar asked Nov 20 '25 10:11

Qwadrox


1 Answers

I would recommend that you use a Mocking Framework like Moq. See my attached example code. ILog is the Interface for Log Class.

public interface ILog {
 void Add(Log log);
}

Your Logger class could look like this:

public class Logger : ILog{
....
public void Add(Log log)
   {
       if (!(log.TimeStamp > DateTime.Now))
        {
            Entries.Add(log);
        }
   }
}
...

You can design your TestCases like this

using Moq;

public class TestLogger 
{
  private Mock<ILog> _logMock;
  private ILog _logger;


  [Setup]
  public void SetUp()
  {

  _logMock = new Mock<ILog>();
  _logMock.Setup(a => a.Add(It.IsAny<Log>())).Verifiable(); // check if method is called

  _logger = _logMock.Object;          
  }

...

  [Test]
  public void SomeTest()
  {
   //test some business logik - inject your logger instance into businesslogic class

   //check if logger was called once
  _logMock.Verify(a => a.Add(It.IsAny<Log>()), Times.Once); 
  }

}


If its not possible to use Moq and external interfaces, you can write your test to check if your Entries List is not empty.

public class Logger
{
  public List<Log> Entries = new List<Log>();

  ...
}
[TestCaseSource(nameof(something))]
    public void AddDebugSetAnonymusUserTest()
    {
        var logger = new Logger();
        logger.AddDebug("Text", null);

       //here is the check if AddDebug was successful
       Assert.AreEqual(1, logger.Entries.Count);

       //check if user was anonymous
       Assert.AreEqual("Anonymous", logger.Entries.FirstOrDefault().UserName);
    }

....
like image 98
user8606929 Avatar answered Nov 21 '25 23:11

user8606929



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!