public void Save() { var logSystem = new LogSystem(); //todo //logSystem.OpenAppend(ILogWriter实例); var result = logSystem.Save(); }
public void Save() { var logSystem = new LogSystem(); // 新建一个ILogWriter的Mock对象,其Object属性即为我们需要的伪对象 var fakeWriter = new Mock <ILogWriter>(); // 新建一个ISource的Mock对象,其Object属性即为我们需要的ISource伪对象 var fakeSource = new Mock <ISource>(); //将ISource伪对象注入到ILogWriter伪对象的属性 fakeWriter.SetupProperty(fake => fake.Source, fakeSource.Object); // 当调用ILogReader接口的Save()方法时,返回true fakeWriter.Setup(fake => fake.Save()).Returns(true); //将伪对象注入到被测试类 logSystem.OpenAppend(fakeWriter.Object); var result = logSystem.Save(); fakeWriter.Verify(fake => fake.Save(), Times.Once); //fakeWriter.Verify(fake => fake.Source, Times.Once); //fakeWriter.VerifyNoOtherCalls(); }