单元测试--Moq

1.基本用法

    public class PersonTest
    {
        [Fact]
        public void GetNames_Moq_ReturnDefault()
        {
            var MocDb = new Mock<IDbHelper>();
            MocDb.Setup(ins => ins.GetList()).Returns(new List<string> { "aa", "bb", "cc" });
            Person p1 = new Person(MocDb.Object);
            var act = p1.GetNames();
            Assert.Contains("aa",act);
        }
    }

    public class Person
    {
        private IDbHelper DbHelper;

        public Person(IDbHelper dbHelper)
        {
            this.DbHelper = dbHelper;
        }
        public IList<string> GetNames()
        {
            return DbHelper.GetList();
        }
    }

    public class SqlHelpler : IDbHelper
    {
        public IList<string> GetList()
        {
            return new List<string> { "a", "b", "c" };
        }
    }

    public interface IDbHelper
    {
        IList<string> GetList();
    }

 

posted @ 2020-01-12 22:59  Programmer_Life  阅读(162)  评论(0编辑  收藏  举报