单元测试和检查私有变量值 [英] Unit testing and checking private variable value

查看:88
本文介绍了单元测试和检查私有变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写单元测试C#,NUnit的和犀牛制品。
这里是一类我测试的相关部分:

I am writing unit tests with C#, NUnit and Rhino Mocks. Here are the relevant parts of a class I am testing:

public class ClassToBeTested
{
    private IList<object> insertItems = new List<object>();

    public bool OnSave(object entity, object id)
    {
        var auditable = entity as IAuditable;
        if (auditable != null) insertItems.Add(entity);

        return false;            
    }
}

我要的OnSave一个电话后,以测试在insertItems值:

I want to test the values in insertItems after a call to OnSave:

[Test]
public void OnSave_Adds_Object_To_InsertItems_Array()
{
     Setup();

     myClassToBeTested.OnSave(auditableObject, null);

     // Check auditableObject has been added to insertItems array            
}

什么是我们的最佳做法?我曾经考虑加入insertItems作为一个属性与公共GET或注射列表为ClassToBeTested,但不知道我应该修改code测试的目的。

What is the best practice for this? I have considered adding insertItems as a Property with a public get, or injecting a List into ClassToBeTested, but not sure I should be modifying the code for purposes of testing.

我已经阅读测试的私有方法和重构的许多职位,但是这是这样一个简单的类,我不知道什么是最好的选择。

I have read many posts on testing private methods and refactoring, but this is such a simple class I wondered what is the best option.

推荐答案

简单的回答是,你应该永远不会访问非公共成员从你的单元测试。这完全违背了有一个测试套件的目的,因为它锁定你进入你可能不希望保持这种方式内部实现细节。

The quick answer is that you should never, ever access non-public members from your unit tests. It totally defies the purpose of having a test suite, since it locks you into internal implementation details that you may not want to keep that way.

较长的答案涉及到怎么办呢?在这种情况下,重要的是理解为什么实现,因为它是重要的(这就是为什么TDD是如此强大,因为我们使用的测试的指定的预期的行为,但我的感觉是你不使用TDD)。

The longer answer relates to what to do then? In this case, it is important to understand why the implementation is as it is (this is why TDD is so powerful, because we use the tests to specify the expected behavior, but I get the feeling that you are not using TDD).

在你的情况,想到的第一个问题是:为什么是IAuditable接口的对象添加到内部列表吗?或者,换句话说,什么是预期的外部可见的这种实现的结果?根据这些问题的答案,的这就是的你需要测试的东西。

In your case, the first question that comes to mind is: "Why are the IAuditable objects added to the internal list?" or, put differently, "What is the expected externally visible outcome of this implementation?" Depending on the answer to those questions, that's what you need to test.

如果您添加IAuditable接口的对象到内部列表中,因为你以后想将它们写入审计日志(只是胡乱猜测),然后调用写入日志的方法和验证预期的数据被写入。

If you add the IAuditable objects to your internal list because you later want to write them to an audit log (just a wild guess), then invoke the method that writes the log and verify that the expected data was written.

如果你是因为你想积累证据对某种约束后的添加IAuditable了对象到内部列表,然后尝试测试。

If you add the IAuditable object to your internal list because you want to amass evidence against some kind of later Constraint, then try to test that.

如果您添加了code为没有可测量的原因,然后再删除它:)

If you added the code for no measurable reason, then delete it again :)

最重要的部分是,它是非常有利于测试的行为的而不是实施的。这也是检验一个更强大和可维护的方式。

The important part is that it is very beneficial to test behavior instead of implementation. It is also a more robust and maintainable form of testing.

不要害怕修改被测系统(SUT)是更容易测试。只要你添加在您的域名意义,并按照面向对象的最佳实践,不存在任何问题 - 你也只是继开/关原理

Don't be afraid to modify your System Under Test (SUT) to be more testable. As long as your additions make sense in your domain and follow object-oriented best practices, there are no problems - you would just be following the Open/Closed Principle.

这篇关于单元测试和检查私有变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆