Base solution for your next web application

Activities of "theedge"

Can you include the exception message you are getting as well as the stack trace?

Hi,

This is a cross post of [https://github.com/aspnetboilerplate/aspnetboilerplate/issues/616]). I am looking for a solution on how to do integration testing such that I can get a transaction for each test that is rolled back at the conclusion of each test.

I am posting here in case others are not monitoring the GitHub feed.

Hi,

Declare the Id property and make it a concatenation or similair of the compound key that exists for your many to many entity.

Hi,

Making a copy of the database to localdb is not an option for each test. Our production DB is a couple of hundred MB ;-). I will give your IUnitOfWorkManager.Begin approach a go and see how I go.

transaction may throw exception on commit! If you rollback it, you will not see such problems. We are only concerned that our code / sp's which may already have their own transactions succeed so the fact that the outer transaction we are simulating fails will not be an issue.

Hi,

We are not using the production database. It is a copy of the production database with the required seed data.

Hi Halil,

I have tried your suggestion with IUnitOfWork but no joy..... Here is my implementation:

//My Repository
public interface IOzCpReferencePortRepository : IRepository<Reference_PortModel, string>
{
}

//My application service
public interface IOzCpReferencePortService : IApplicationService
{
    void DonoAddPort();
}



//Adapter class to make ABP play nice with NUnit
public class AbpApplicationServiceTestsAdapter : OzAbpTestsAdapterBase
{
    protected override void AddModules(ITypeList<AbpModule> aModules)
    {
        base.AddModules(aModules);

        //Add testing modules. Dependant modules are automatically added.
        aModules.Add<OzCruisingPlatformApplicationModule>();
        aModules.Add<OzCruisingPlatformDataModule>();
    }
}

//My NUnit test class
public class OzCpReferencePortService : OzCruisingPlatformAppServiceBase, IOzCpReferencePortService
{
    private readonly IOzCpReferencePortRepository _Repository;

    /// <summary>
    ///     Class constructor
    /// </summary>
    /// <param name="aRepository">Repository associated with the service</param>
    public OzCpReferencePortService(IOzCpReferencePortRepository aRepository)
    {
        //Save params supplied
        _Repository = aRepository;

        //Configure auto mapper
        ConfigureAutoMapper();
    }

   public void DonoAddPort()
    {
        //Dummy for testing
        _Repository.Insert(new Reference_PortModel
                           {
                               CountryCode = "AU",
                               MarketName = "Dono",
                               PortCode = "DDD",
                               PortName = "Dono",
                               VisaRequired = true
                           });
    }
}

[TestFixture]
public class ReferencePortTests : OzTestsBase
{
    private AbpApplicationServiceTestsAdapter _AbpApplicationTestsAdapter;
    private IOzCpReferencePortService _ReferencePortService;
    private IUnitOfWork _UnitOfWork;

    /// <summary>
    ///     Called at the commencement of all the unit tests.
    /// </summary>
    [TestFixtureSetUp]
    public void Init()
    {
        //Spin up the adapter class to register all the necessary with the IocManager
        _AbpApplicationTestsAdapter = new AbpApplicationServiceTestsAdapter();

        //Create our reference to the reference port service
        _ReferencePortService = _AbpApplicationTestsAdapter.LocalIocManager.Resolve<IOzCpReferencePortService>();
    }

    [Test]
    public void Dono()
    {
        _ReferencePortService.DonoAddPort();
    }
	
    /// <summary>
    ///     Called before each test.
    /// </summary>
    [SetUp]
    public void TestSetup()
    {
        //Ensure all interactions run within a transaction of their own so that our changes are not persisted to the database
        _UnitOfWork =  _AbpApplicationTestsAdapter.LocalIocManager.Resolve<IUnitOfWork>();
        _UnitOfWork.Begin(new UnitOfWorkOptions());
    }

    /// <summary>
    ///     Called after each test.
    /// </summary>
    [TearDown]
    public void TestTearDown()
    {
        _UnitOfWork.Dispose();
    }
}

I spin up a transaction in TestSetup() and dispose of it in TestTearDown which are called on either side of the test Dono(). However the record added in Dono() still exists in the database after the unit of work has been disposed off (ie rolled back). I cannot use it in a using statement other than putting that in each test which I don't want to do, hence the explicit call to Dispose().

Can you shed any light on this?

Hi Halil,

Doh! Sorry that was a dumb mistake on my part. Doing that I now successfully have the test running in a transaction and no changes being made to the database, BUT.... I cannot see any of my changes made in the transaction when I make a successive call.

[SetUp]
        public void TestSetup()
        {
            //Ensure all interactions run within a transaction of their own so that our changes are not persisted to the database
            IUnitOfWorkManager unitOfWorkManager = _AbpApplicationTestsAdapter.LocalIocManager.Resolve<IUnitOfWorkManager>();
            _UnitOfWork = unitOfWorkManager.Begin(new UnitOfWorkOptions
                                                  {
                                                      IsolationLevel = IsolationLevel.ReadUncommitted
                                                  });
        }

and now the test that adds a record and then uses the service to find the record:

[Test]
        public void Dono()
        {
            _ReferencePortService.DonoAddPort();

            OzCpGetPortOutput port = _ReferencePortService.GetPort(new OzCpGetPortInput
                                                                   {
                                                                       PortCode = "DDD"
                                                                   });

            //The following should NOT be possible as I have just inserted a PortCode "DDD" via DonoAddPort()
            if (port.Item == null)
            {
                   //Get in here all the time no matter what I set the UoW isolation level to
            }
        }

The only time GetPort() returns the record is if I do not do unitOfWorkManager.Begin() ie with NO transaction for the test. So even for an isolation level allowing dirty reads I cannot get the record.

What am I missing here?

Thanks Halil, missed the SaveChanges() bit. I now have this working as I want it.

Can you supply some code?

Hi,

  • Assume I have 3 tables named TableA, TableB, TableC
  • And a stored proc name StoredProc that makes use of all 3 tables with some form of JOIN

I would now like to access the StoredProc in my implementation of IApplicationService from the service layer.

So typically I have done:

public interface ITableARepository : IRepository<TableA, long>
 {
     //Internally uses DbContext to call the SP  
      void CallStoredProc();
 }

public interface IMyService : IApplicationService
 {
        void CallMyStoredProc();
}

public class MyService : IMyService
{   
  private  ITableARepository _TableArepository; 

  public MyService(ITableARepository aTableARepository)    
   {
         _TableARepository = aTableARepository;
   }

          public void CallMyStoredProc()
         {
                _TableARepository.CallStoredProc()
         }
}

However as the stored proc can access all the 3 tables invariably someone goes looking for it off TableBRepository or TableCRepository instead of TableARepository. As I don't want to pass a DBContext to my application service layer as that is a concrete and not an interface, what is a better way to do this?

Namely what is a "good" way to allow access to stored procedures without "tying" them arbitrarily to a repository interface? All the examples on ABP don't talk about stored procedures so I am looking for some guidance.

All pointers and suggestions appreciated.

Showing 1 to 10 of 47 entries