public interface IViewBase { int ID { get; } string Name { get; set; } event EventHandler Init; }
public class PresenterBase { public string Status { get; protected set; } public PresenterBase(IViewBase view) { View = view; Status = "Constructed"; } protected IViewBase View{ get; private set;} public void BindEventsInternal() { View.Init += view_Init; BindEvents(); } protected virtual void BindEvents() {} private void view_Init(object sender, EventArgs e) { Status = "Init"; } }
private MockFactory _factory; [TestInitialize] public void TestInitialize() { _factory = new MockFactory(); } [TestMethod] public void TestMockMVP() { Mock<IViewBase> _View = _factory.CreateMock<IViewBase>(); //设置事件 _View.Expects.One.EventBinding(v => v.Init += null); //模拟方法 //若是泛型则写法如下 //_View.Expects.One.EventBinding<类型>(v => v.Init += null); PresenterBase presenter = new PresenterBase(_View.MockObject); Assert.IsNotNull(presenter); }
NMock学习系列(二)--- NMock在MVP架构系统的单元测试中的应用
原文地址:http://blog.csdn.net/eye_cng/article/details/45049087