1. 通过 Nuget 安装

2.   官网下载(官网不行点这里)

3.   帮助文档

商业版和免费版区别概览

MockingContainer

测试类准备:一般来说也是业务类


    public class ClassUnderTest
{
private IFirstDependency firstDep;
private ISecondDependency secondDep; public ClassUnderTest(IFirstDependency first, ISecondDependency second)
{
this.firstDep = first;
this.secondDep = second;
} public IList<object> CollectionMethod()
{
var firstCollection = firstDep.GetList(); return firstCollection;
} public string StringMethod()
{
var secondString = secondDep.GetString(); return secondString;
}
} public interface IFirstDependency
{
IList<object> GetList();
} public interface ISecondDependency
{
string GetString();
}

  单元测试


        [TestMethod]
public void ShouldMockDependenciesWithContainer()
{
// ARRANGE
// Creating a MockingContainer of ClassUnderTest.
// To instantiate the system uder test (the container) you should use the Instance property
// For example: container.Instance.
var container = new MockingContainer<ClassUnderTest>(); string expectedString = "Test"; // Arranging: When the GetString() method from the ISecondDependecy interface
// is called from the container, it should return expectedString.
container.Arrange<ISecondDependency>(
secondDep => secondDep.GetString()).Returns(expectedString); // ACT - Calling SringMethod() from the mocked instance of ClassUnderTest
var actualString = container.Instance.StringMethod(); // ASSERT
Assert.AreEqual(expectedString, actualString);
}

  需要说明的是MockingContainer构造函数有一个可选参数AutoMockSettings,其中AutoMockSettings最主要的一个作用的是当ClassUnderTest有多个构造函数时,指定合适的构造函数来实例化对象

     当业务类中有一个无参构造函数,一个有参构造函数,在没有设置AutoMockSettings的情况下会默认执行无参构造函数,

    可以像下面这样来指定需要的构造函数

        AutoMockSettings settings = new AutoMockSettings
{
ConstructorArgTypes = new Type[]{
typeof(IFirstDependency),typeof(ISecondDependency)
}
}; // ARRANGE
// Creating a MockingContainer of ClassUnderTest.
// To instantiate the system uder test (the container) you should use the Instance property
// For example: container.Instance.
var container = new MockingContainer<ClassUnderTest>(settings);

  

     

JustMock Lite (Free Mocking Framework For .net)的更多相关文章

  1. Testing with a mocking framework (EF6 onwards)

    When writing tests for your application it is often desirable to avoid hitting the database.  Entity ...

  2. 什么是Mocking framework?它有什么用?

    原位地址:http://codetunnel.com/blog/post/what-is-a-mocking-framework-why-is-it-useful 今天我想讲下关于mocking fr ...

  3. Mocking framework

    [译] 什么是Mocking framework?它有什么用? 原位地址:http://codetunnel.com/blog/post/what-is-a-mocking-framework-why ...

  4. What is a mocking framework? Why is it useful?

    Today I want to talk about mocking frameworks and why they are useful. In order to do that I first n ...

  5. 什么是Mocking framework?它有什么用?(转)

    今天我想讲下关于mocking frameworks,并且解释下他为什么有用处.我将给你们展示用和不用mocking framework两种测试方法. 假设我们已经有了一个Driver类: publi ...

  6. Googletest - Google Testing and Mocking Framework

    Googletest - Google Testing and Mocking Framework https://github.com/google/googletest

  7. 利用Mocking Framework 单元测试Entity Framework

    一.前言 在实际编写程序时,往往需要与数据库打交道,在单元测试中直接使用数据库又显得太重,如果可以方便的编写一些测试数据,这样更易于检测功能.如何模拟数据库行为便是本篇的主题.微软有教程说明Moq E ...

  8. 第一篇:Entity Framework 简介

    先从ORM说起吧,很多年前,由于.NET的开源组件不像现在这样发达,更别说一个开源的ORM框架,出于项目需要,以及当时OOP兴起(总不至于,在项目里面全是SQL语句),就自己开始写ORM框架.要开发O ...

  9. Entity Framework版本历史概览

    转自:http://www.cnblogs.com/fecktty2013/archive/2014/09/26/entityframework-overview.html EF版本 .net fra ...

随机推荐

  1. 事务环境下的CombGuid

    一直使用osharp,osharp3使用的是combguid,代码如下 /// <summary> /// 返回Guid用于数据库操作,特定的时间代码可以提高检索效率 /// </s ...

  2. WebGrid Helper with Check All Checkboxes

    WebGrid Helper with Check All Checkboxes myEvernote Link Tuesday, September 13, 2011ASP.NET ASP.NET ...

  3. XPath使用小结

    参考资料: http://www.w3school.com.cn/xpath/xpath_nodes.asp

  4. asp.net mvc ClaimsIdentity 授权研究 (还是测试版 有bug)

      安装 Microsoft.Owin.Host.SystemWeb Identity.Core Microsoft.Owin.Security.Cookies 在是startup.cs做如下修改 p ...

  5. php中的正则函数主要有三个-正则匹配,正则替换

    php中变量的声明? 由于php声明变量的时候, 不支持使用 var关键字, 又不能直接写一个变量名字, 孤零零的放在那里, 所以, 在php中声明变量的方式, 同时也是给变量初始化的形式, 即: & ...

  6. Linux启动流程CentOS6

    1.运行级别 0 关机 1 单用户模式,可以想象为Windows的安全模式,主要用与系统修复 2 不完全的命令行模式,不含NFS服务 3 完全的命令行模式,就是标准字符界面 4 系统保留 5 图像模式 ...

  7. C#集合u

    List<T> 列表(动态数组),相当于C++的 vector Queue<T> 队列,先进先出 Stack<T> 栈,先进后出 LinkedList<T&g ...

  8. jQuery.validator 详解二

    前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示的内部实现 一.插件结构(组织方式) 在讲 ...

  9. python __future__ package的几个特性

    我学习python过程, 和学习其它编程知识一样, 不是先读大部头书系统学习, 而是看博客和直接实践, 慢慢将这些知识点连成线, 再扩展到面. 这个过程缺点和优点都很明显. 缺点是, 有些知识点可能因 ...

  10. CF #305(Div.2) D. Mike and Feet(数学推导)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...