博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RhinoMocks简单范例
阅读量:5794 次
发布时间:2019-06-18

本文共 3337 字,大约阅读时间需要 11 分钟。

using System; namespace MockTest{    public interface IBBB    {        int Number { get; }        int Compute(int j);        int GetNumber();    }    public  class BBB : IBBB     {        public virtual  int Compute(int j)        {            throw new NotImplementedException();        }        public int GetNumber()        {            throw new NotImplementedException();        }        public int Number        {            get { throw new NotImplementedException(); }        }    }    public class AAA    {        private IBBB _bbb;        public IBBB Bbb        {            get { return _bbb; }            set { _bbb = value; }        }        public AAA(IBBB a)        {            _bbb = a;        }        public AAA() { }        public int Compute(int i, int j)        {            return i + _bbb.Compute(j);        }    }}

 *******************************************************************************************************

 

using Microsoft.VisualStudio.TestTools.UnitTesting;using MockTest;using Rhino.Mocks;namespace UnitTest{     [TestClass]    public class AAATest    {        [TestMethod]        public void RhinoStubTest()        {                        //接口.方法(指定参数20,返回指定值1)            IBBB ib1 = Rhino.Mocks.MockRepository.GenerateMock
(); ib1.Stub(x => x.Compute(20)).Return(1); Assert.AreEqual(1, ib1.Compute( 20)); Assert.AreEqual(0, ib1.Compute(200)); //接口.方法(无参数) IBBB ib2 = Rhino.Mocks.MockRepository.GenerateMock
(); ib2.Stub(x => x.GetNumber()).Return(2); Assert.AreEqual(2, ib2.GetNumber()); //接口.属性 IBBB ib3 = Rhino.Mocks.MockRepository.GenerateMock
(); ib3.Stub(x => x.Number).Return(3); Assert.AreEqual(3, ib3.Number); //类.方法 BBB b4 = Rhino.Mocks.MockRepository.GenerateMock
(); b4.Stub(x => x.Compute(20)).Return(4); Assert.AreEqual(4, b4.Compute(20)); //任意参数,返回固定值 IBBB ib5 = Rhino.Mocks.MockRepository.GenerateMock
(); ib5.Stub(x => x.Compute(Arg
.Is.Anything)).Return(5); Assert.AreEqual(5, ib5.Compute(9999)); //条件参数,返回固定值 IBBB ib6 = Rhino.Mocks.MockRepository.GenerateMock
(); ib6.Stub(x => x.Compute(Arg
.Is.LessThan(10))).Return(6); Assert.AreEqual(6, ib6.Compute(9)); Assert.AreEqual(0, ib6.Compute(11)); } [TestMethod] public void RhinoMockTest_DependentObject_IsBy_SuccessCall() { IBBB ib = Rhino.Mocks.MockRepository.GenerateMock
(); //设置.协同对象.预期行为 ib.Expect(x => x.Compute(20)).Return(1); AAA aaa = new AAA(); aaa.Bbb = ib; aaa.Compute(11,20); //判断协同对象.预期行为是否调用 ib.VerifyAllExpectations(); } [TestMethod] [Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedException(typeof(Rhino.Mocks.Exceptions.ExpectationViolationException))] public void RhinoMockTest_DependentObject_IsBy_ErrorCall() { IBBB ib = Rhino.Mocks.MockRepository.GenerateMock
(); //设置.协同对象.预期行为 ib.Expect(x => x.Compute(20)).Return(1); AAA aaa = new AAA(); aaa.Bbb = ib; aaa.Compute(11, 200); //判断协同对象.预期行为是否调用 ib.VerifyAllExpectations(); } }}

 

转载于:https://www.cnblogs.com/dude/p/3793866.html

你可能感兴趣的文章
Angular.js中的$injector服务
查看>>
构建之法读书笔记01
查看>>
linux - lsof 命令最佳实践
查看>>
kafka性能测试
查看>>
现实世界的Windows Azure:h.e.t软件使用Windows Azure削减50%的成本
查看>>
深入.net框架
查看>>
聚合类新闻client产品功能点详情分析
查看>>
js设置定时器
查看>>
数据库除运算
查看>>
LeetCode--112--路径总和
查看>>
DeviceIOControl与驱动层 - 缓冲区模式
查看>>
感悟贴2016-05-13
查看>>
vim使用教程
查看>>
跨vlan通信-----单臂路由技术
查看>>
百度编辑器ueditor 光标位置的坐标
查看>>
DEV-C++ 调试方法简明图文教程(转)
查看>>
VS2017+EF+Mysql生成实体数据模型(解决闪退的坑)
查看>>
C++多态、继承的简单分析
查看>>
库克称未来苹果用户可自己决定是否降频 网友:你是在搞笑吗?
查看>>
6倍性能差100TB容量,阿里云POLARDB咋实现?
查看>>