【问题标题】:Unit Testing with Moq in Visual studio 2012在 Visual Studio 2012 中使用 Moq 进行单元测试
【发布时间】:2013-08-26 11:42:33
【问题描述】:

我在 Visual Studio 2012 (Microsoft.VisualStudio.TestTools.UnitTesting) 中使用 moq 库 (Moq),想知道如何使用 Moq 为以下 Employee 类编写单元测试类。

   publc class Employee

    {

      private Deparment _objectA; //Class
      protected IManager  _objectB; //Interface

      public Employee(Deparment objectA, IManager  objectB)
       {

         _objectA=objectA;
         _objectB=objectB;

       }

       internal void EmpMethod1()
       {

       }

       internal void EmpMethod2()
       {

        }

       internal void EmpMethod3()
       {

       }

       }
   }

    public interface IManager

     {
          Manager GetManager(int id);

          Void AddManager(MgrName);
      }

【问题讨论】:

  • 你想测试哪些公共方法?
  • Employee 类中有内部方法,我想我们可以测试内部方法吗?

标签: oop unit-testing c#-4.0 visual-studio-2012 moq


【解决方案1】:

这是来自:

How to Mock the Internal Method of a class?

以下文章中还介绍了一种解决方法,即在您的项目中使用属性:

Mocking Internal Types with Moq

您还可以考虑使用非代理模拟库,例如 TypeMock、JustMock 或 Microsoft Fakes。

【讨论】:

    猜你喜欢
    • 2011-12-24
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    相关资源
    最近更新 更多