How to Mock the data source from startup

Nov 25, 2014 at 5:56 PM
This is the third project I'm working on that's based on Eisk. I've really enjoyed working with it.

I sure hope this makes sense...

Note: I'm working on a WebAPI 2 service right now.

The current situation is that I'm developing and doing preliminary development in a place where I don't have access to the test or production database. (I can get to them but not from the dev environment for now) What I'd like to do is have the Eick system create, populate and then use an in memory database. What I'd like is for someone with a good knowledge of how Eick works to clue me in on how to go about that. All I'm looking for is a list of steps. Like:
  • In Global.asax.cs, move the line DataInitializer.Init(); to after the DependencyInjectorInitializer.Init();
  • In DataInitializer.cs, put a branch to select which data source to use (Pull that from the web.config)
  • Create a new initializer that does something.
  • Do some more stuff...
The main mental problem I'm having (related to this anyway) is how to connect to that virtual in memory database in real time. As in, I don't know how to set the context to it.

Why do I want to do this?
Besides the above mentioned issue about getting to the database(s) I also want to expand the scope of automated testing. I've got unit tests that are Mocking the data source and filling that data source from xml files. I've got some integration tests that are using the same test data in the xml files. Now, I'm going to try and consume the data from the WebAPI 2 service but have the system mock all the data from those same xml files. In the end I want to populate the test database from those same xml files for testing.

[assembly: AssemblyTitle("Eisk.MVCApp")]
[assembly: AssemblyVersion("5.6.100.0")]
[assembly: AssemblyFileVersion("5.6.100.0")]

Thanks for the help.

MArk B.
Coordinator
Nov 27, 2014 at 7:51 AM
Yes, you can mock the data source, shown on the example below. Check the EISK MVC 7.0 test project for details.
        Mock<DatabaseContext> _mockDbContext;
        Employee _employee;
        EmployeesController _employeeController;
        FakeEmployeeSet _fakeEmployeeDbSet;

        [TestInitialize]
        public void TestInitialize()
        {
            DependencyHelper.Initialize();
            IUnityContainer container = DependencyHelper.Container;
            _mockDbContext = new Mock<DatabaseContext>();
            container.RegisterInstance<DatabaseContext>(_mockDbContext.Object);
            _employeeController = container.Resolve<EmployeesController>();
            _fakeEmployeeDbSet = new FakeEmployeeSet();
        }

        [TestCleanup()]
        public void TestCleanup()
        {
            DependencyHelper.ClearContainer();
        }
        
        #endregion

        [TestMethod]
        public void Edit_Negative_Test_Post_Mock_Test()
        {
            //Arrange
            _employee = TestDataHelper.CreateEmployeeWithValidData();
            _employee.Id = 100;
            _fakeEmployeeDbSet.Add(_employee);

            _employee = TestDataHelper.CreateEmployeeWithValidData();
            _employee.Id = 200;
            _fakeEmployeeDbSet.Add(_employee);

            _mockDbContext.Setup(db => db.EmployeeRepository).Returns(_fakeEmployeeDbSet);

            //Act
            _employeeController.FireValidationForModel(_employee);
            _employeeController.Edit(_employee);

            //Assert
            Assert.IsTrue(EmployeeAddressMustBeUnique.IsErrorAvalilableIn(_employeeController, _employee));
       
        }
Dec 1, 2014 at 10:45 PM
Thank you for the reply Joycsc,

I've found and utilized the mocking in the unit test project. What I've been unable to figure out is where to do a similar thing but have the main EISK application use the mocked data source.

As I've found, Unity is creating the database context at run time and uses Constructor Injection to set the context for the database in the controller.

For example, when I'm debugging the the site and put a break point in the constructor for the EmployeesController the database context is pointing to the database indicated in the DatabaseContext connection string set in the connnectionStrings section of the Web.config.
public EmployeesController(DatabaseContext databaseContext)
{
  _dbContext = databaseContext;
}
That's what I'm trying to circumvent. I don't want the running application to get the database in the connection, I want it to get another mocked database.


Another way to think of this is that maybe I want to change the database on the fly. Maybe put a link right up there next to the "Reset Data" link which when clicked would tell Unity to get its act together and point at a different database, you know, because I told it to. If I could find out where that branch statement would go I might be able to figure out the rest by myself.

Thanks again,

MArk B.