对调用基类方法的方法进行单元测试 [英] Unit testing a method that calls a base class method

查看:155
本文介绍了对调用基类方法的方法进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的方法

I have a method that looks something like this

public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("someName", this.someName);
            info.AddValue("someName1", this.someName1);
            
            base.GetObjectData(info, context);
        }



如果我正在对该方法进行单元测试,是否应该保留它不变,还是应该通过某种方式对其进行重构来尝试自己测试"base.GetObjectData()"?
如何对该方法进行单元测试?

我包含了一条语句,现在可以检查参数是否为null.



If I''m unit testing this method, should I just leave it as it is or should I try and test ''base.GetObjectData()'' by iteself by refactoring it out somehow?
How would I unit test this method?

I included a statement that now checks to see if a parameter in null.

if (info == null)
           {
               throw new ArgumentNullException("info");
           }


我进行了这样的测试


and I included a test like so

[TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetObjectData_NullParameters_ThrowsException()
        {
            Respondent myRespondent = new Respondent(new RespondentInformation());
            SerializationInfo info = null;
            StreamingContext context = new StreamingContext();
            myRespondent.GetObjectData(info, context);

        }



但是还没有测试的余地吗?我还能测试什么?



but isn''t there more to test? What else can I test? Shouldn''t I be testing the base.GetObjectData'' method?

推荐答案

IMHO:
1.您应该独立地测试基类,之前测试派生类.
2.测试一个空参数很好,但是在给定有效参数的情况下,您还应该测试一下函数的结果(不是这个,但是不是void的其他函数)是否是您期望的结果,并且状态为调用该方法后的对象也是您期望的对象.

花几个小时阅读单元测试:它会有所回报.

希望这会有所帮助,
巴勃罗.
IMHO:
1. You should test the base class independently, before you test the derived class.
2. Testing for a null parameter is nice, but you should also test that, given valid parameters, the result of your function (not this one, but others that are not void) is the one you expect, and the state of the object after calling the method is also the one you expect.

Spend a few hours reading on unit tests: it will pay off.

Hope this helps,
Pablo.


这篇关于对调用基类方法的方法进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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