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. yii2的urlManager配置

    网址伪静态是一个非常常用的网站需求,Yii2可以非常简单地进行配置. 首先,在配置文件config/main.php的'components' 段中,加入如下设置:'urlManager'=>a ...

  2. python快排算法

    通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列. ...

  3. http模拟请求工具

    http模拟请求工具: postman(chrome应用) Request Maker(chrome插件) Request Maker(网站:http://www.requestmaker.com/) ...

  4. MySql避免重复插入记录

    今天用python抓取数据入库需要避免重复数据插入,在网上找了一些方法: 方案一:使用ignore关键字 如果是用主键primary或者唯一索引unique区分了记录的唯一性,避免重复插入记录可以使用 ...

  5. ng指令之 ng-repeat 篇

    1>数据绑定     ng-repeat可以绑定数组和JSON对象数据.从下图可以看出控制器的scope()函数得到的对象与controller('ctrlName',['$scope',fun ...

  6. 【采集层】Kafka 与 Flume 如何选择--转自悟性的博文

    [采集层]Kafka 与 Flume 如何选择 收藏 悟性 发表于 2年前 阅读 23167 收藏 16 点赞 4 评论 1 摘要: Kafka, Flume 采集层 主要可以使用Flume, Kaf ...

  7. 再说vundle: 完全vim字符编程的四个必须插件 - zen coding 和emmet插件的使用

    一个常识: 基本上vim插件的配置文集都是放在对应插件目录 的/autoload/ plugin_name.vim 文件中的 有四个必要/必须的插件,实现vim完全的字符界面的编程: NERDTree ...

  8. move_upload_file 因为文件字符集编码iconv引起的问题

    对 包含中文的文件 进行操作时提示 Invalid argument? 包括: 这里的move_uploaded_file和 fopen等操作都是如此. 而且用了字符编码转换后, iconv('utf ...

  9. Django动态下载文件

    前台提交查询条件,下载符合条件的EXCEL数据文件,后端视图中使用 xlwt 库来返回,如: objs = Units.objects.all() # 创建 Workbook 时,如果需要写入中文,请 ...

  10. KNN-实现文本分类

    现在大多程序.关于算法的都封装的差不多了... 所以很多程序猿很少来进行深入来研究了... 以前也想过自己好好学习下.但是理论确实难以下咽.怪我喽... 这次项目中需要用到了.要实现对文本进行分类的一 ...