Exemplo n.º 1
0
        public void AppendLine2()
        {
            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);

            // 伪造ILogWriter接口的CurrentLine属性
            fakeWriter.SetupProperty(fake => fake.CurrentLine);
            fakeWriter.Object.CurrentLine = 3;
            // 当连续调用ILogWriter接口的AppendLine方法,并输入任意string类型参数时,触发回调使伪对象CurrentLine属性的值+1
            fakeWriter.Setup(fake => fake.AppendLine(It.IsAny <string>()))
            .Callback(() => fakeWriter.Object.CurrentLine++);

            //将伪对象注入到被测试类
            logSystem.OpenAppend(fakeWriter.Object);

            logSystem.AppendLine("运行正常");

            Assert.AreEqual(4, logSystem.Writer.CurrentLine);
        }
Exemplo n.º 2
0
        public void AppendLine2()
        {
            var logSystem = new LogSystem();

            //todo
            //logSystem.OpenAppend(ILogWriter实例);

            logSystem.AppendLine("运行正常");

            Assert.AreEqual(4, logSystem.Writer.CurrentLine);
        }