?????????????????? ??????Action???????????????????????????????TransactionScope????.
????????????????xUnit ??? ??????????????? ?????????xUnit 1.8?汾. ????MbUnit??NUnit??[RollBack]?????????????. ??????????????????xUnit????????????. ??????????? xUnit ???????????:
1:      /// <summary>
2:      /// Database unit testing with xUnit demo
3:      /// </summary>
4:      /// <remarks>http://wintersun.cnblogs.com </remarks>
5:      public class TestDbWithxUnit
6:      {
7:          [Fact]
8:          [AutoRollback]
9:          public void Test_Add_One_Enity()
10:          {
11:              //arrange
12:              var employee = this.CreateNewEmployee();
13:              var employRepository = RepositoryHelper.GetEmployeeRepository();
14:
15:              //act
16:              employRepository.Add(employee);
17:              employRepository.Save();
18:
19:              //assert
20:              var employeelist =
21:                 employRepository.Repository.Find(e => e.EmployeeID == employee.EmployeeID);
22:              Assert.NotNull(employeelist);
23:              Assert.Equal(new List<Employee> { employee }?? employeelist.ToList());
24:          }
25:  }
??????????????????????????????AutoRollback??Attribute??????? ??????????????xunit.extensions.dll
???????????????????????????????????? ???????????????????в???÷????????. ???????Mock?????????????????? ???′???????????????Moqv4.0.20926 ?? xUnit ????????????:
1:          [Fact]
2:          public void TestWithMoq()
3:          {
4:              //arrange
5:              var mock = new Mock<IRepository<Employee>>();
6:              Employee employee = this.CreateNewEmployee();
7:              var list = new List<Employee>() { employee };
8:
9:              mock.Setup(ep => ep.Add(It.IsAny<Employee>()))
10:                  .Callback(()=>Console.WriteLine("Add()"));
11:              mock.Setup(ep => ep.Find(It.IsAny<Expression<Func<Employee?? bool>>>()))
12:                  .Returns(list);
13:              mock.Setup(ep => ep.Save())
14:               .Callback(() => Console.WriteLine("Save()"));
15:              var employeeRespository = mock.Object;
16:
17:              //act
18:              employeeRespository.Add(employee);
19:              employeeRespository.Save();
20:
21:              //verify method
22:              mock.Verify(ep => ep.Add(It.IsAny<Employee>()));
23:              mock.Verify(ep => ep.Save());
24:
25:              //assert
26:              var employeelist =
27:                employeeRespository.Find(e => e.EmployeeID == employee.EmployeeID);
28:              Assert.NotNull(employeelist);
29:              Assert.Equal(new List<Employee> { employee }?? employeelist.ToList());
30:          }
????Employee??????? IRepository<T>??????????????interface?? ???martinfowler??Repository??;  ????????Mock?????е?Add??Save?????? ????ú?????Verify???????Mock??????б????????  ?????????Assert????. ??????.
???????????Mock???? ????????????????????????????????????Fake???????????????????? ???????Unity 2.1 ?????:
1:          [Fact]
2:          public void TestWithDIContainer()
3:          {
4:              //arrange
5:              var employee = this.CreateNewEmployee();
6:              var list = new List<Employee>() { employee };
7:              var employeeRespository = GetRepositoryInstance("for testing");
8:
9:              //act
10:              employeeRespository.Add(employee);
11:              employeeRespository.Save();
12:
13:              //assert
14:              var employeelist =
15:                employeeRespository.Find(e => e.EmployeeID == employee.EmployeeID);
16:              Assert.NotNull(employeelist);
17:              Assert.Equal(new List<Employee> { employee }?? employeelist.ToList());
18:          }
19:
20:          private IRepository<Employee> GetRepositoryInstance(string name)
21:          {
22:              //Initial container
23:              var container = new UnityContainer();
24:              container.RegisterType<IRepository<Employee>?? EFRepository<Employee>>();
25:              container.RegisterType<IRepository<Employee>?? FakeRepository<Employee>>("for testing");
26:              //....
27:              return container.Resolve<IRepository<Employee>>(name);
28:          }
?????????????????????? ?????????????????????????private??????; ??????????????IOC/DI??????. FakeRepository<Employee>??????????IRepository<T>??????? ?????????????????в?????壬???????????. ???忴??????????.
??????????????????????????????????????е?????????????:
????1. ???TransactionScope??????????????
????2. ???xUnit????AutoRollback?????????????
????3. ???Moq????????????????????
????4. ???Unity???????Fake?????????????????????