NUnit.Mocks实战测试实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Mocks;
namespace VS2008Text
{
    [TestFixture]
    public class NumersFixture
    {
        [Test]
        [Explicit]
        //[Ignore]
        [Category("groupA")]
        //[ExpectedException(typeof(DivideByZeroException))]
        public void TestAdd()
        {
            double result;
            double expected = 7;
            Form1 testclass = new Form1();
            result = testclass.addNum(2, 5);

            //int cs = 0;
            //int zero = 4 / cs;

            Assert.AreEqual(expected, result);
            StringAssert.Contains("包含", "测试是否包含指定字符串", "测试是否包含A");
            StringAssert.StartsWith("A", "ABC", "测试A开头");
            StringAssert.EndsWith("C", "ABC", "测试C结尾");
            StringAssert.AreEqualIgnoringCase("ABCD", "abcd");

            //MMock应用
              DynamicMock da = new DynamicMock(typeof(Form1));
            da.SetReturnValue("getB", 4);  //设置固定返回值
              da.ExpectAndReturn("getB", 3, 1);  //根据参数返回相应值
              da.ExpectAndReturn("getB", 4, 2);  //根据参数返回相应值
              Form1 ff = (Form1)da.MockInstance; //获得Form1实例
              Console.WriteLine(da.Call("getB", 1).ToString()); //根据参数调用返回值

              expected = 11;
            //result = testclass.addNum(7, ff.getB());  //固定返回值调用方法
              result = testclass.addNum(7, (int)da.Call("getB", 2));  //根据参数调用返回值
            
              Assert.AreEqual(expected, result);

            Console.WriteLine("测试结束");
        }
    }
}

你可能感兴趣的:(C#)