一、前言
使用Mock框架进行单元测试,能够使用当前系统已经开发的接口方法模拟数据。(未写完,慢慢完善)
二、例子
1.引用Moq
2.定义一个接口和方法作为测试
public interface IAnimal { //创造动物,返回是否创造成功 bool CreateAnimal(string name); //方法参数中有输出参数 bool AnimalDance(string name, out string msg); string Eat(string food); int GetCount(); int FindEven(int even); int FindRange(int num); string FindRegex(string str); string Name { get; set; } }
3.Test
static void Main(string[] args) { Console.WriteLine("------------------------------------------------------------------------"); //1.根据接口创建mock var mock = new Mock<IAnimal>(); //2.配置将要模拟的方法,调用方法传入参数,当传入熊猫的时候返回true,其他参数则返回fale mock.Setup(a => a.CreateAnimal("熊猫")).Returns(true); ; //3.调用mock配置好的方法 Console.WriteLine(mock.Object.CreateAnimal("熊猫")); //true Console.WriteLine(mock.Object.CreateAnimal("大熊猫"));//false Console.WriteLine("------------------------------------------------------------------------"); string msg = "跳舞"; //配置将要模拟的方法,调用方法传入参数(也传入输出参数),当传入孔雀的时候返回true,其他参数则返回fale mock.Setup(a => a.AnimalDance("孔雀", out msg)).Returns(true); string outmsg = ""; Console.WriteLine(mock.Object.AnimalDance("孔雀", out outmsg)); //true,输出参数为跳舞 Console.WriteLine(outmsg); Console.WriteLine(mock.Object.AnimalDance("大孔雀", out outmsg));//false,输出参数为跳舞 Console.WriteLine(outmsg); Console.WriteLine("------------------------------------------------------------------------"); //配置调用方法的时候参数为白菜的时候抛出异常 mock.Setup(a => a.Eat("白菜")).Throws(new ArgumentException("错了")); Console.WriteLine(mock.Object.Eat("青菜")); //Console.WriteLine(mock.Object.Eat("白菜")); //抛出异常 Console.WriteLine("------------------------------------------------------------------------"); //在每一次调用返回不同的值,callback() ; mock.Setup(a => a.GetCount()).Returns(() => count).Callback(() => count++); Console.WriteLine(mock.Object.GetCount()); Console.WriteLine(mock.Object.GetCount()); Console.WriteLine(mock.Object.GetCount()); Console.WriteLine(mock.Object.GetCount()); Console.WriteLine("------------------------------------------------------------------------"); //无论输入什么字符串,都是输出食物两个字 It.IsAny<string>() mock.Setup(a => a.Eat(It.IsAny<string>())).Returns((string s) => "食物"); Console.WriteLine(mock.Object.Eat("面包")); Console.WriteLine("------------------------------------------------------------------------"); //输入的数字是偶数,则返回该数字本身,否则为默认值0 It.Is<int> mock.Setup(a => a.FindEven(It.Is< == ))).Returns((int b) => b); ; i < ; i++) { Console.WriteLine(mock.Object.FindEven(i)); } Console.WriteLine("------------------------------------------------------------------------"); //匹配范围的值 It.IsAny<string>() mock.Setup(a => a.FindRange(It.IsInRange<, , Range.Inclusive))).Returns((int b) => b); ; i < ; i++) { Console.WriteLine(mock.Object.FindRange(i)); } Console.WriteLine("------------------------------------------------------------------------"); //匹配正则表达式 It.IsAny<string>() mock.Setup(a => a.FindRegex(It.IsRegex("[4-7]"))).Returns((string b) => b); Console.WriteLine(mock.Object.FindRegex(")); Console.WriteLine(mock.Object.FindRegex(")); //只有5符合,只输出5 Console.WriteLine(mock.Object.FindRegex(")); Console.WriteLine("------------------------------------------------------------------------"); //属性 mock.Setup(s => s.Name).Returns("属性哦"); Console.WriteLine(mock.Object.Name); Console.ReadKey(); }