如何在单元测试中使用模拟对象,并且仍然使用代码覆盖率? [英] How can I use Mock Objects in my unit tests and still use Code Coverage?

查看:132
本文介绍了如何在单元测试中使用模拟对象,并且仍然使用代码覆盖率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我开始在我的单元测试中引入Mock对象的概念.特别是我正在使用Moq框架.但是,我注意到的一件事是,突然间我正在使用此框架测试的类的代码覆盖率显示为0%.

Presently I'm starting to introduce the concept of Mock objects into my Unit Tests. In particular I'm using the Moq framework. However, one of the things I've noticed is that suddenly the classes I'm testing using this framework are showing code coverage of 0%.

现在我了解到,由于我只是在模拟类,因此它没有运行实际的类本身..但是,我该如何编写这些测试并使Code Coverage返回准确的结果呢?我是否需要编写一组使用Mocks的测试,以及一组直接实例化该类的测试.

Now I understand that since I'm just mocking the class, its not running the actual class itself....but how do I write these tests and have Code Coverage return accurate results? Do I have to write one set of tests that use Mocks and one set to instantiate the class directly.

也许我做错了事而没有意识到吗?

Perhaps I am doing something wrong without realizing it?

以下是我尝试对名为"MyClass"的类进行单元测试的示例:

Here is an example of me trying to Unit Test a class called "MyClass":

using Moq;
using NUnitFramework;

namespace MyNameSpace
{
    [TestFixture]
    public class MyClassTests
    {

        [Test]
        public void TestGetSomeString()
        {
            const string EXPECTED_STRING = "Some String!";

            Mock<MyClass> myMock = new Mock<MyClass>();
            myMock.Expect(m => m.GetSomeString()).Returns(EXPECTED_STRING);

            string someString = myMock.Object.GetSomeString();

            Assert.AreEqual(EXPECTED_STRING, someString);
            myMock.VerifyAll();

        }

    }

    public class MyClass
    {
        public virtual string GetSomeString()
        {
            return "Hello World!";
        }
    }
}

有人知道我应该做些什么吗?

Does anyone know what I should be doing differently?

推荐答案

您没有正确使用模拟对象.使用模拟对象时,您打算测试代码如何与其他对象交互,而无需实际使用实际对象.请参见下面的代码:

You are not using your mock objects correctly. When you are using mock objects you meant to be testing how your code interacts with other objects without actually using the real objects. See the code below:

using Moq;
using NUnitFramework;

namespace MyNameSpace
    {
        [TestFixture]
        public class MyClassTests
        {

            [Test]
            public void TestGetSomeString()
            {
                const string EXPECTED_STRING = "Some String!";

                Mock<IDependance> myMock = new Mock<IDependance>();
                myMock.Expect(m => m.GiveMeAString()).Returns("Hello World");

                MyClass myobject = new MyClass();

                string someString = myobject.GetSomeString(myMock.Object);

                Assert.AreEqual(EXPECTED_STRING, someString);
                myMock.VerifyAll();

            }

        }

        public class MyClass
        {

            public virtual string GetSomeString(IDependance objectThatITalkTo)
            {
                return objectThatITalkTo.GiveMeAString();
            }
        }

        public interface IDependance
        {
            string GiveMeAString();
        }
    }

当您的代码仅返回没有任何逻辑的字符串时,它似乎没有做任何有用的事情.

It doesn't look like it is doing anything useful when your code is just returning a string without any logic behind it.

如果您GetSomeString()方法执行了某些逻辑,这些逻辑可能会根据IDependdance.GiveMeAString()方法的返回来更改输出字符串的结果,那么真正的力量就来了,那么您可以看到您的方法如何处理错误数据.从IDependdance接口发送.

The real power comes if you GetSomeString() method did some logic that may change the result of the output string depending on the return from the IDependdance .GiveMeAString() method, then you can see how your method handles bad data being sent from the IDependdance interface.

类似的东西:

 public virtual string GetSomeString(IDependance objectThatITalkTo)
 {
     if (objectThatITalkTo.GiveMeAString() == "Hello World")
         return "Hi";
     return null;
 }

现在,如果您在测试中有此行:

Now if you have this line in your test:

myMock.Expect(m => m.GiveMeAString()).Returns(null);

您的GetSomeString()方法会发生什么?

这篇关于如何在单元测试中使用模拟对象,并且仍然使用代码覆盖率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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