Simple.Data

Simple.Data has always been designed with "testing as a core feature" and supports unit testing your whole application stack using the InMemoryAdapter found in the Core namespace.

Testing Basics : The InMemoryAdapter

The great thing about the InMemoryAdapter is that you can test all your data access code against per test definied data, in memory. So it's very fast and you don't have to change a thing in your data access code. Simplest thing is probably just to explain this with an example:

Let's say that you have this simple method to retrieve a Customer by Id

private dynamic GetCustomer(int id)
{
  var db = Database.Open();
  return db.Test.FindById(id);
}

If you want to test that method call you need to call into the underlying database. Not only is that slow but also does that mean that you need to seed the database with some data first... And remember to clean it up later.

With the InMemoryAdapter that is much cleaner:

[Test]
public void should_get_a_customer()
{
    // Arrange
    // Set up the InMemoryAdapter
    var adapter = new InMemoryAdapter();
    Database.UseMockAdapter(adapter);

    // Insert some test data
    var db = Database.Open();
    db.Test.Insert(Id: 1, Name: "Alice");

    // Act
    var record = GetCustomer(1);

    // Assert
    Assert.IsNotNull(record);
    Assert.AreEqual(1, record.Id);
    Assert.AreEqual("Alice", record.Name);
}

The first two lines configure the InMemoryAdapter and tells Simple.Data to use it with the static method .UseMockAdapter([adapter]). All subsequent calls to Simple.Data will use that adapter

Then we insert some data into the "database" (which now is run in-memory) with the usual syntax for inserting.

The method GetCustomer knows nothing about the InMemoryAdapter-stuff and the code is run just as it was written. Only this time the InMemoryAdapter is used and the data we just inserted is returned.