如何编写具有只读修饰符的属性的单元测试? [英] How to write unit test hat a property that has readonly modifier on it?

查看:39
本文介绍了如何编写具有只读修饰符的属性的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是个愚蠢的问题

public class File
{
    public Metadata metadata
    {
        get
        {
            return _metadata;
        }
    }
    private readonly Metadata _metadata;

    #region public

    File () { ... }

    Foo () { ... }

    #endregion
}

现在我想知道我是否需要编写单元测试来验证_metadata是只读的情况,以及如何

Now I am wondering whether I need to write unit tests to verify the case that the _metadata is readonly, and how

推荐答案

不,您通常不需要为编译器声明和检查的东西编写测试.例如,您通常不会编写只能调用具有正确类型的方法等的测试.

No, you don't generally need to write tests for things which are declared to and checked by the compiler. For example, you don't generally write tests that you can only call methods with the right types, etc.

当然,如果您有多种类型,并且您想检查它们的所有属性是否都是只读的,例如,这是有道理的.(例如,这个接口的每个实现都应该是不可变的"——只读属性至少是检查它的开始.)

Of course, if you have several types and you want to check that all their properties are readonly, for example, that makes sense. (e.g. "every implementation of this interface should be immutable" - readonly properties is at least a start to checking that.)

这篇关于如何编写具有只读修饰符的属性的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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