简体   繁体   English

单元测试无效方法

[英]Unit testing a void method

I understand that you can unit test a void method by checking for its effects. 我了解您可以通过检查void方法的效果来对其进行单元测试。 However, looking at the loadConfigFile method in this code: 但是,请看以下代码中的loadConfigFile方法:

    internal XmlDocument configData;

    public ConfigFile()
    {
        configData = new XmlDocument();
    }

    /// <summary>
    /// Load config file into memory
    /// </summary>
    /// <param name="filename">path and filename of config file</param>
    public void loadConfigFile(string filename) 
    {
        if(string.IsNullOrEmpty(filename))
            throw new System.ArgumentException("You must specify a filename");

        try 
        {
            configData.Load(filename);
        }
        catch(Exception ex)
        {
            throw new Exception("Config file could not be loaded",ex);
        }
    }

It loads a config file into a private field - which needs to be kept private so that developers do not modify the value directly. 它将配置文件加载到私有字段中-需要将该私有文件保持私有状态,以便开发人员不直接修改该值。 Instead the modification will be done through setConfigValue and getConfigValue methods (which I would assume need to be tested separately). 而是通过setConfigValue和getConfigValue方法(我认为需要单独测试)来完成修改。

Given this, how would I test that the loadConfigFile actually worked? 鉴于此,我将如何测试loadConfigFile实际起作用? As I cant access the private configData field. 由于我无法访问私有configData字段。

Where else in that class is configData used? 在该类的其他地方configData使用configData吗?

If, say, you have a method like 假设您有类似的方法

public string GetValue()
{
    return configData.GetsomeDataFromThis;
}

Then I suggest you have a test like so: 然后,我建议您进行如下测试:

public void ReadValueFromLoadedConfigData()
{
    // Arrange.
    const string ExpectedValue = "Whatever";

    var sut = new ConfigFile();

    sut.loadConfigFile(@"C:\PathToTheConfigFile");

    // Act.
    string actual = sut.GetConfigValue();

    // Assert.
    Assert.AreEqual(ExpectedValue, actual);
}

In your tests, try to test only the public interactions, since going deep into the class, by having to read values of private fields, means your class isn't test friendly. 在测试中,请尝试仅测试公共交互,因为深入该类,因为必须读取私有字段的值,这意味着您的课程不友好。

You could test that getConfigValue returned the values loaded from the file, basically. 您基本上可以测试getConfigValue返回从文件加载的值。

Just because you test setConfigValue / getConfigValue in other tests doesn't mean that you can't use them in the test for loadConfigFile . 仅仅因为在其他测试中测试了setConfigValue / getConfigValue并不意味着您不能在针对loadConfigFile的测试中使用它们。

As an aside, I'd strongly urge you to follow .NET naming conventions, starting your method names with capital letters. 顺便说一句,我强烈建议您遵循.NET命名约定,以大写字母开头的方法名称。 (I'd also urge you to make your field private rather than internal...) (我也敦促您将您的领域设为私有而非内部...)

A unit test is basically a way to say "given this input, verify that this happened", it is not a substitute for verifying that your application is in a valid state. 单元测试从根本上说“给出了这个输入,确认了这个事情发生了”,但是并不能代替验证您的应用程序处于有效状态。

In the example, the loadConfigFile method throws an exception if the precondition is not met, or the load operation failed. 在此示例中,如果不满足前提条件,或者装入操作失败,则loadConfigFile方法将引发异常。 This will then be detected in the unit test, which will fail. 然后,这将在单元测试中检测到,这将失败。

If any other validation is needed on the config beyond that no exception is thrown, that should be handled in the class itself. 如果配置上还需要其他任何验证,则不会引发任何异常,这应在类本身中处理。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM