使用NMock的虚拟工厂方法帮助NUnit测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用NMock的虚拟工厂方法帮助NUnit测试相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Text; using NMock2; using NUnit.Framework; namespace NUnitSandbox { public interface IThingInterface { int GetThingNumber(); } public class RealThing { public int GetThingNumber() { return 13; } } public class ClassToBeTested { protected virtual IThingInterface GetThing() { } public int WhatIsTheThingNumber() { IThingInterface iThing = GetThing(); return iThing.GetThingNumber(); } } /// <summary> /// This class inherits from ClassToBeTested, so it can override the Virtual Thing Factory method (GetThing). /// </summary> [TestFixture] public class TestClass : ClassToBeTested { /// <summary> /// This is the key. /// </summary> protected override IThingInterface GetThing() { IThingInterface mockThing = mocks.NewMock<IThingInterface>(); Expect.AtLeastOnce.On(mockThing).Method("GetThingNumber").Will(Return.Value(42)); return mockThing; } [Test] public void TestGetThingNumber() { Assert.AreEqual(42, this.WhatIsTheThingNumber(), "Get Thing Number value should return 42 as specified by the mock."); } } }
以上是关于使用NMock的虚拟工厂方法帮助NUnit测试的主要内容,如果未能解决你的问题,请参考以下文章
JAVA设计模式-创建模式-工厂模式-工厂方法模式/虚拟构造子模式/多态性工厂