• Moq练习


    本文参考

    http://www.cnblogs.com/haogj/archive/2011/06/24/2088788.html

    Moq适合于TDD的项目,小项目初期应该不太适合使用,有些浪费时间了!

    NuGet 分别添加 NUnit 和 Moq

    install-package nunit -project 测试项目名称

    install-package moq -project 测试项目名称

    public class Person
        {
            public string Id;
            public string FirstName;
            public string LastName;
            public Person(string newId, string fn, string ln)
            {
                Id = newId;
                FirstName = fn;
                LastName = ln;
            }
        }
    
    
    
        public interface IPersonRepository
        {
            List<Person> GetPeople();
            Person GetPersonById(string id);
        }
    
        public class PersonService
        {
            private IPersonRepository personRepos;
            public PersonService(IPersonRepository repos)
            {
                personRepos = repos;
            }
            public List<Person> GetAllPeople()
            {
                return personRepos.GetPeople();
            }
            public List<Person> GetAllPeopleSorted()
            {
                List<Person> people = personRepos.GetPeople();
                people.Sort(delegate(Person lhp, Person rhp)
                {
                    return lhp.LastName.CompareTo(rhp.LastName);
                });
                return people;
            }
            public Person GetPerson(string id)
            {
                try
                {
                    return personRepos.GetPersonById(id);
                }
                catch (ArgumentException)
                {
                    return null; // no person with that id was found
                }
            }
        }
    View Code

    Moq

    [TestFixture]
        public class NUnitTest
        {
    
            private Mock<IPersonRepository> mo = new Mock<IPersonRepository>();
    
            private Person onePerson = new Person("1", "Wendy", "Whiner");
            private Person secondPerson = new Person("2", "Aaron", "Adams");
            private List<Person> peopleList;
    
            /// <summary>
            ///     填充基础数据
            /// </summary>
            [SetUp]//每个测试方法被调用之前执行
            [Category("Mock")]
            public void Init()
            { 
                peopleList = new List<Person>();
                peopleList.Add(onePerson);
                peopleList.Add(secondPerson);
                
                
            }
    
            [Test]
            [Category("Mock")]
            public void TestGetAllPeople()
            {
                //模拟使用接口方法及返还数据
                mo.Setup(m => m.GetPeople()).Returns(peopleList);
    
                PersonService service = new PersonService(mo.Object);
    
                Assert.AreEqual(2, service.GetAllPeople().Count);
            }
    
    
            [Test]
            [Category("Mock")]
            public void TestGetAllPeopleSorted()
            {
                mo.Setup(m => m.GetPeople()).Returns(peopleList);
                
                PersonService service = new PersonService(mo.Object);
    
                var p  = service.GetAllPeopleSorted()[0];
    
                Assert.AreEqual("Adams", p.LastName);
            }
    
            [Test]
            [Category("Mock")]
            public void TestGetSinglePersonWithValidId()
            {
                mo.Setup(m => m.GetPersonById(It.Is<string>(s => s == "1")))
                    .Returns(onePerson);
    
                PersonService service = new PersonService(mo.Object);
    
                var p = service.GetPerson("1");
    
                Assert.IsNotNull(p);
                Assert.AreEqual(p.Id, "1");
            }
    
            [Test]
            [Category("Mock")]
            public void TestGetSinglePersonWithInalidId()
            {
                mo.Setup(m => m.GetPersonById(It.IsAny<string>()));
    
                PersonService service = new PersonService(mo.Object);
                Assert.IsNull(service.GetPerson(null));
            }
    
    
    
    
    
    
        }
  • 相关阅读:
    七牛上传图片
    Mysql数据库分布式事务XA详解
    PostgreSQL查询表名称及表结构
    利用DataSet分页方法 小宝马的爸爸
    Flex4中的皮肤(4):使用SkinPart约束Skin 小宝马的爸爸
    Flex4中使用WCF 小宝马的爸爸
    Flex4中的皮肤(3):使用组件数据 小宝马的爸爸
    (转)Flex4中的皮肤(1):自定义SkinnableComponent 小宝马的爸爸
    一起学ASP.NET中如何使用存储过程 小宝马的爸爸
    从宫二的李为看处世哲学 小宝马的爸爸
  • 原文地址:https://www.cnblogs.com/hope-help/p/4104062.html
Copyright © 2020-2023  润新知