Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Wednesday, 27 April 2016

Inspired by:
http://farm-fresh-code.blogspot.co.uk/2014/02/lightweight-integration-testing-with.html

which was itself inspired by a Jimmy Bogard article
https://lostechies.com/jimmybogard/2012/10/18/isolating-database-data-in-integration-tests/

If you get the MSDTC error
http://toddmeinershagen.blogspot.co.uk/2014/05/data-integration-tests-and-transactions_25.html

Here is the start of an example just using rollback transactions

[TestClass] public class TransactionTests { [TestMethod] public void Insert_User_and_test_transaction_rolls_back() { var user = new User { CreatedDate = DateTime.Now, DisplayName = "Todd Smith4", Email = "todd4@asdf.com", Active = true }; var sql = "INSERT INTO dbo.Users (CreatedDate, DisplayName, Email, Active) VALUES (@CreatedDate, @DisplayName, @Email, @Active)"; db.Execute(sql, user); var retrievedUser = db.Query<User>("select * from users where Email = @email", new { email = user.Email }).FirstOrDefault(); Assert.AreEqual(user.DisplayName, retrievedUser.DisplayName); Assert.AreEqual(user.Active, retrievedUser.Active); Assert.AreEqual(user.CreatedDate.ToString(), retrievedUser.CreatedDate.ToString()); }

asdf

private TransactionScope scope; private DbConnection db; private DapperUserRepositoryTestContext c; private bool rollBackTransaction = true; // before each test [TestInitialize] public void SetUp() { // direct access to the db db = new SqlConnection( ConfigurationManager.ConnectionStrings["DapperMangoTestingConnectionString"].ConnectionString); if (rollBackTransaction) scope = new TransactionScope(); // going through the repo c = new DapperUserRepositoryTestContext(); } // after each test [TestCleanup] public void TearDown() { if (rollBackTransaction) scope.Dispose(); db.Dispose(); }
| | # 
# Tuesday, 16 August 2011

Testing the repository and the ORM.

Code Snippet
namespace OSIM.IntegrationTests.OSIM.Core.Persistence {
    // using the Unit Test project to inherit off Specification, which inherits off NBehave.Spec.NUnit
    public class when_using_the_item_type_repository : Specification {
        protected IItemTypeRepository _itemTypeRepository;
        protected StandardKernel _kernel;

        protected override void Establish_context() {
            base.Establish_context();
            _kernel = new StandardKernel(new IntegrationTestModule());
            _itemTypeRepository = _kernel.Get<IItemTypeRepository>();
        }
    }

    public class and_attempting_to_save_and_read_a_value_from_the_datastore : when_using_the_item_type_repository {
        private ItemType _expected;
        private ItemType _result;

        protected override void Establish_context() {
            base.Establish_context();
            // creates different data each time
            // if for some reason old data doesn't get cleared out.
            _expected = new ItemType { Name = Guid.NewGuid().ToString() };
        }

        protected override void Because_of() {
            var itemTypeId = _itemTypeRepository.Save(_expected);
            _result = _itemTypeRepository.GetById(itemTypeId);
        }

        [Test]
        public void then_the_item_type_saved_to_the_database_should_equal_the_item_type_retrieved() {
            _result.Id.ShouldEqual(_expected.Id);
            _result.Name.ShouldEqual(_expected.Name);
        }
    }
}

asdf

Code Snippet
<configuration>
  <appSettings>
    <add key="localDb" value="Data Source=SSDLAPTOP; Initial Catalog=OSIM.Dev; Integrated Security=True"/>
  </appSettings>
</configuration>

Every time the test runs, it recreates the db:

image

asdf

image

asdf

Code Snippet
namespace OSIM.IntegrationTests {
    public class IntegrationTestModule : NinjectModule {
        public override void Load() {
            // when ninject is asked for an instance of IItemTypeRepository
            // return ItemTypeRepository
            Bind<IItemTypeRepository>().To<ItemTypeRepository>();

            Bind<ISessionFactory>().ToProvider
                (new IntegrationTestSesssionFactoryProvider());
        }
    }

    public class IntegrationTestSesssionFactoryProvider : Provider<ISessionFactory> {

        protected override ISessionFactory CreateInstance(IContext context) {
            var sessionFactory = Fluently.Configure()
                                    .Database(MsSqlConfiguration.MsSql2008
                                        .ConnectionString(c => c.Is(ConfigurationManager.AppSettings["localDb"])).ShowSql())
                                        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ItemTypeMap>()
                                            .ExportTo(@"e:\temp"))
                                        .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
                                        .BuildSessionFactory();
            return sessionFactory;
        }
    }
}

Wiring up FNH using ninject.

| | #