NUnit.Mocks実戦テスト例

1683 ワード

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(" ");
        }
    }
}