简体   繁体   English

测试ClassA中的方法是否已从ClassA中的另一个方法调用

[英]Test if method in ClassA has been called from another method in ClassA

It is possible to test if a method has been called using Moq and dependency injection . 可以测试是否已使用Moq和依赖注入调用方法。 However, is it possible to test if one method in a class calls another within the same class? 但是,是否可以测试类中的一个方法是否在同一个类中调用另一个方法?

For example, I want to test that if I log a certain exception, that an information message is logged as well. 例如,我想测试一下,如果我记录某个异常,那么也会记录一条信息消息。

The method is: 方法是:

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);
}

This was my attempt at unit testing it. 这是我尝试进行单元测试。 The test fails, is there any way that it can it 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 don't believe I can pass ClassA as a dependency into ClassA. 由于InfoError方法属于同一个类(ClassA),我不相信我可以将ClassA作为依赖项传递给ClassA。 So does it not need tested? 那么它不需要测试吗?

The best you're going to be able to do is to make Info virtual . 你能做的最好的事情就是让Info virtual This will allow you to create a Mock<Logger> , set CallBase = true , and verify that Info was called. 这将允许您创建Mock<Logger> ,设置CallBase = true ,并验证是否已调用Info

var mock = new Mock<Logger> 
{
    CallBase = true
};

mock.Object.Error("test" + counter++, new Exception("test" + counter), Int64.MaxValue);

mock.Verify(m => m.Info(It.IsAny<string>(), It.IsAny<int>()));

This way, you're still calling the actual implementation of Error , but you've used Moq to verify the Info method was called. 这样,您仍然调用Error的实际实现,但是您已经使用Moq来验证调用了Info方法。

It feels like you're trying to test the wrong thing. 感觉你正试图测试错误的东西。 It's not really important that the Info method on your class is called from the Error method, what's important is that the behaviour of the Info method occurs. Error方法调用类上的Info方法并不重要,重要的是Info方法的行为发生。 How it happens is an implementation detail of the class. 它是如何发生的是该类的实现细节。

If I had a math class with two functions: 如果我有一个包含两个函数的数学类:

public int Mult(int x, int y) {
    return x*y;
}

public int Sqr(int x) {
    return Mult(x,y);
}

I wouldn't test that calling Sqr called out to the Mult function, I would test Sqr(4)==16 . 我不会测试调用Sqr调用Mult函数,我会测试Sqr(4)==16 It doesn't matter if that calculation takes place in the Sqr method, or in another method of the class. 如果计算发生在Sqr方法或类的另一个方法中,则无关紧要。

Whilst @Andrew's solution is probably what you're after, mocking the class you're testing tends to lead to tightly coupled, brittle tests. 虽然@ Andrew的解决方案可能就是你所追求的,但是嘲笑你正在测试的课程往往导致紧密耦合的脆弱测试。

If it's impractical to test the call by observing it's side effects, then it may be a sign that the implementation could use a bit of refactoring. 如果通过观察它的副作用来测试调用是不切实际的,那么它可能表明实现可能会使用一些重构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 ClassA.Method() 调用为 ClassB.Method() 但只影响 ClassA 值,两个类都源自相同的 BaseClass - Calling a ClassA.Method() as ClassB.Method() but only affecting ClassA values, both classes are deriving from the same BaseClass ClassA中的方法是否可能由ClassB中的事件触发? - Is it possible for a method in ClassA to be triggered by an event in ClassB? 无法从“方法组”转换为 Func <list<classa> &gt; 实例化新任务时</list<classa> - cannot convert from 'method group' to Func<List<ClassA>> when instantiating a new Task 将ClassA中的事件处理程序添加到ClassB中调用的事件中 - Add event handler from ClassA to an event called in ClassB 当我们可以直接创建ClassA对象并调用classA的printA方法时,为什么要隐藏时需要使用new关键字 - Why we need to use new keyword if hiding is intended, when we can directly create ClassA object and call printA method of classA 从ClassA转换为ClassB的设计指南 - Design guidelines for converting from ClassA to ClassB 有效地创建List <ClassB> 从列表 <ClassA> ? - efficiently creating List<ClassB> from List<ClassA>? 检查字典的值 <DateTime, List<ClassA> &gt;是否有使用linq的特定类型的ClassA? - Check if the values of Dictionary<DateTime, List<ClassA>> has a specific type of ClassA using linq? 如何通知已从中调用方法的对象 - How to notify object from which a method has been called 当ClassB从ClassA继承时,如何将List <ClassB>转换为List <ClassA>? - How to cast List<ClassB> to List<ClassA> when ClassB inherits from ClassA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM