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();
}