本文参考

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

相关文章:

  • 2022-01-22
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2021-05-09
  • 2021-08-04
  • 2021-07-02
  • 2021-09-11
猜你喜欢
  • 2022-12-23
  • 2021-06-04
  • 2021-10-14
  • 2021-08-19
  • 2021-08-05
  • 2021-08-31
  • 2022-12-23
相关资源
相似解决方案