zoukankan      html  css  js  c++  java
  • RhinoMocks简单范例



    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<IBBB>();
                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<IBBB>();
                ib2.Stub(x => x.GetNumber()).Return(2);
                Assert.AreEqual(2, ib2.GetNumber());
    
                //接口.属性
                IBBB ib3 = Rhino.Mocks.MockRepository.GenerateMock<IBBB>();
                ib3.Stub(x => x.Number).Return(3);
                Assert.AreEqual(3, ib3.Number);
    
                //类.方法
                BBB b4 = Rhino.Mocks.MockRepository.GenerateMock<BBB>();
                b4.Stub(x => x.Compute(20)).Return(4);
                Assert.AreEqual(4, b4.Compute(20));
    
                //任意参数,返回固定值
                IBBB ib5 = Rhino.Mocks.MockRepository.GenerateMock<IBBB>();
                ib5.Stub(x => x.Compute(Arg<int> .Is.Anything)).Return(5);
                Assert.AreEqual(5, ib5.Compute(9999));
    
                //条件参数,返回固定值
                IBBB ib6 = Rhino.Mocks.MockRepository.GenerateMock<IBBB>();
                ib6.Stub(x => x.Compute(Arg<int>.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<IBBB>();
                //设置.协同对象.预期行为
                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<IBBB>();
                //设置.协同对象.预期行为
                ib.Expect(x => x.Compute(20)).Return(1);
    
                AAA aaa = new AAA();
                aaa.Bbb = ib;
                aaa.Compute(11, 200);
                //判断协同对象.预期行为是否调用
                ib.VerifyAllExpectations();
    
            }
    
     
        }
    }
  • 相关阅读:
    压缩流GZipStream
    委托和事件
    .NET垃圾回收机制
    程序集相关知识
    生活
    poj 3767 I Wanna Go Home
    fw:Python GUI的设计工具
    How to configure an NTP client and server on CentOS/RedHat
    FW:Tripwire
    Bash if 判断 集合
  • 原文地址:https://www.cnblogs.com/dude/p/3793866.html
Copyright © 2011-2022 走看看