You can check if a method using Moq and dependency injection has been called. However, is it possible to verify that one method in a class calls another in the same class?
For example, I want to verify that if I register an exception, an informational message is also logged.
Method:
public void Error(string message, Exception exception, long logId = 0) { var int32 = (int)logId; Info("Id was converted to an int so that it would fit in the log: " + logId, int32); Error(message, exception, int32); }
It was my attempt at testing the module. The test fails, is there a way that this can be done?
void logging_an_error_with_a_long_id_also_logs_info() { var mock = new Mock<ILogger>(); var testedClass = new Logger(); var counter = 0; testedClass.Error("test" + counter++, new Exception("test" + counter), Int64.MaxValue); mock.Verify(m => m.Info(It.IsAny<string>(), It.IsAny<int>())); }
Since the Info and Error methods are in the same class (ClassA), I do not believe that I can pass ClassA as a dependency on ClassA. So it does not need to be checked?
Hoppe source share