Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Tuesday, August 16, 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.

| | #