有什么方法可以模拟BeginTransaction进行单元测试

时间:2019-04-30 09:52:31

标签: c# asp.net-core

我已经模拟了数据库中的一些表,还使用了begin事务,如何在代码中模拟Begin事务?

我已经在API中使用了begin事务,但是我没有为我为测试用例编写的代码了解如何实现这些事务的模拟:

[Fact]
public void InsertPeople_OkResult()
{
    //ILoggerMocking
    var serviceProvider = new ServiceCollection()
    .AddLogging()
    .BuildServiceProvider();

    var factory = serviceProvider.GetService<ILoggerFactory>();

    var logger = factory.CreateLogger<CreateProfileAPIController>();

    //IConfigurationMock
    var datasourcemck = new Mock<IConfigurationSection>();
    datasourcemck.Setup(x => x.Value).Returns("3");

    var entmk = new Mock<IConfigurationSection>();
    entmk.Setup(x => x.Value).Returns("2000");

    var confg = new Mock<IConfigurationRoot>();
    confg.Setup(s => s.GetSection("DataSourceId")).Returns(datasourcemck.Object);
    confg.Setup(s => s.GetSection("EntityId")).Returns(entmk.Object);

    //Act
    peopleRepository = new PeopleRepository(createProfileTestCasesPreparations.mockContext.Object, confg.Object, logger);
    //Arrange
    var data = new SunBook.Models.ViewModel.PersonalDetailsModel()
    {
        People = new SunBook.Models.People()
        {
            FirstName = "Pj",
            LastName = "ff",
            Gender = "male",
            LastChangedOn = DateTime.Now,
            Suffix = "sr",
            PeopleTagId = 2


        },
        EntityPeople = new SunBook.Models.EntityPeople()
        {
            Title = "dev",
            Department = "labs",
            LastChangedOn = DateTime.Now,
            DataSourceId = 3,
            FromDate = DateTime.Now

        },
        PeopleTagId = 2,
        TagId = 1


    };
    peopleRepository.AddPersonalDetails(data);
    var cont = new ContactDetailsModel()
    {

        PeopleAddress = new List<SunBook.Models.PeopleAddress>()
    {
        new SunBook.Models.PeopleAddress()
        {
            PersonId =data.People.PersonId,
            Address1 ="Vzag",
            Address2 ="MVP",
            AddressTypeId =1,
            City ="Vizag",
            Country ="Ind",
            LastChangedOn =DateTime.Now,
            DataSourceId =3,
            ZipCode ="53545",
            State ="ME"
        }
    },
        PeopleEmail = new List<SunBook.Models.PeopleEmail>()
    {
        new SunBook.Models.PeopleEmail()
        {
            EmailAddress ="abc@gmail.com",
            DataSourceId =3,
            EmailTypeId =1,
            LastChangedOn =DateTime.Now,
            PersonId =data.People.PersonId,
            IsPrimary =true
        }
    },
        PeoplePhones = new List<SunBook.Models.PeoplePhone>()
    {
        new SunBook.Models.PeoplePhone()
        {
            PersonId =data.People.PersonId,
            PhoneNumber ="985646684",
            DataSourceId =3,
            LastChangedOn =DateTime.Now,
            PhoneTypeId =1,
            IsPrimary =true
        }
    },
        PersonId = data.People.PersonId,
        PeopleTagId = 2

    };
    peopleRepository.AddContactDetails(cont);

    //Assert
    Assert.NotEqual<int>(0, data.People.PersonId);
}

任何人都可以帮助我,如何在上述案例中模拟交易

1 个答案:

答案 0 :(得分:0)

您可以使用System.Transactions.TransactionScope代替开始交易

using System.Transactions;

using (var transactionScope = new TransactionScope())
{
  // your db operations ...

  transactionScope.Complete();
}

您不需要模拟System.Transactions.TransactionScope,单元测试就可以正常工作。