I
have fallen for Simple.Data. Big time. It’s so terse and concise
that you almost lose the need for data access abstractions altogether.
It’s just … there for you. Just see for yourself here. And when have this power at
your fingertips it’s easy to forget the testing story.
How should I use Simple.Data so that I still can write unit and
acceptance tests with code using it?
In my unit-tests I do not want to call the database, of course, because
that would slow my unit-level tests down. Also if I do automated
acceptance I want to go end-to-end, testing the full stack of the
application. But there the database access will slow me down, often in
the form of a network hop as well.
Have no fear!
Mr Rendle
have set us up with a couple of ways to mock out the actual call to the
database on the lowest level, but still go through Simple.Data.
In this blog post I want to show you a couple of ways to write
unit-tests that doesn’t hit the database, but still use Simple.Data.
Using the XmlMockAdapter
When you install Simple.Data via
NuGet you might be
surprised to find a lot of packages there. There one for each adapter
(supporting 7 different databases and counting), a core package, a
sample and our friend
Simple.Data.Mocking. This package installs,
according to the description; XML-based mocking for the Simple.Data data
access library.
I create a Class Library and NuGet’ed the following packages:
I can now write some tests. Lets try to simply call a mocked database,
in a known state and return some rows for us:
```
1: [TestFixture]
```
```
2: public class CallingSimpleDataDirectlyTests
```
```
3: {
```
```
4: private XmlMockAdapter _mockAdapter;
```
```
5:
```
```
6: [TestFixtureSetUp]
```
```
7: public void MyTestInitialize()
```
```
8: {
```
```
9: _mockAdapter = new XmlMockAdapter(
```
```
10: @"
```
```
11: <Users _keys=""Id"" Id=""System.Int32"" Key=""System.Guid"">
```
```
12: <User Id=""1"" Email=""marcus@marcusoft.net"" Password=""secretPassword""/>
```
```
13: <User Id=""2"" Email=""kalle@marcusoft.net"" Password=""qwerty""/>
```
```
14: <User Id=""3"" Email=""john@marcusoft.net"" Password=""1q2s3e4r""/>
```
```
15: </Users>
```
```
16: ");
```
```
17:
```
```
18: MockHelper.UseMockAdapter(_mockAdapter);
```
```
19: }
```
```
20:
```
```
21: [Test]
```
```
22: public void FindByEmail_ShouldFindRecord()
```
```
23: {
```
```
24: // Arrange
```
```
25: var db = Database.Open();
```
```
26:
```
```
27: // Act
```
```
28: var user = db.Users.FindByEmail("marcus@marcusoft.net");
```
```
29:
```
```
30: // Assert
```
```
31: Assert.AreEqual(1, user.Id);
```
```
32: Assert.AreEqual("marcus@marcusoft.net", user.Email);
```
```
33: Assert.AreEqual("secretPassword", user.Password);
```
```
34: }
```
```
35: }
```
So here (and yes, I’ve stolen the sample from the Simple.Data
repository’s test project) we create a XmlMockAdapter in the Setup. On
row 9 through 16 we load it up with some XML data. This can of course be
read from a file if you want to, but then your pushing the envelope on
what a **unit**-test is, in my opinion.
Line 18 is important – that tells Simple.Data to use our \_mockAdapter.
So that the next time we open a database the Xml mock adapter will be
used. In a later version of the code, I’ve peeked, you can write
something like this:
```
1: Database.UseMockAdapter(_mockAdapter);
```
which in my opinion is much clearer. However it works in the same way.
On line 25 our test code begins by opening the database for us, which
will use the XmlMockAdapter, remember. The rest of the test is simple
data access code using Simple.Data to pull back a record and validating
against it. Note that we’re using “Users” that is the name of the
Xml-element Users as our table name.
Here are some other tests using the same setup method above, and
performing insert, updates and deletes:
```
1: [Test]
```
```
2: public void Delete_should_delete_a_user()
```
```
3: {
```
```
4: // Act
```
```
5: int numberDeleted = Database.Default.Users.Delete(Id: 2);
```
```
6:
```
```
7: // Assert
```
```
8: numberDeleted.Should().Equal(1);
```
```
9: }
```
```
10:
```
```
11: [Test]
```
```
12: public void Update_should_update_the_email()
```
```
13: {
```
```
14: // Act
```
```
15: const string email = "marcus@marcusoft.net";
```
```
16: int updated = Database.Default.Users.UpdateById(Id: 3, Email: email);
```
```
17:
```
```
18: // Assert
```
```
19: updated.Should().Equal(1);
```
```
20: string updatedEmail = Database.Default.Users.FindById(3).Email;
```
```
21: updatedEmail.Should().Equal(email);
```
```
22: }
```
```
23:
```
```
24: [Test]
```
```
25: public void Insert_should_insert_new_entry()
```
```
26: {
```
```
27: // Arrange
```
```
28: var db = Database.Default;
```
```
29: var numberOfUsersBefore = db.Users.All().Count;
```
```
30:
```
```
31: dynamic newUser = new ExpandoObject();
```
```
32: newUser.Id = 40;
```
```
33: newUser.Email = "newemail@marcusoft.net";
```
```
34: newUser.Password = "newpassword";
```
```
35:
```
```
36: // Act
```
```
37: db.Users.Insert(newUser);
```
```
38:
```
```
39: // Assert
```
```
40: var numberOfUsersAfter = db.Users.All().Count;
```
```
41: }
```
I used the Database.Default property which simply gives us the default
database in the .config, or in our case just the database with the
mock-adapter. It’s just a shorthand for:
```
1: var db = Database.Open();
```
```
2: db.Users.WhatEver;
```
Using a repository
In start of this post I said that the Simple.Data helps to write less
code. Maybe so that we don’t need to use a separate repository. But
maybe you want to use one anyway. To be able to mock out the repository
when you’re testing a class using the repository for example.
Simple.Data actually helps us to test the repository as well, using the
XmlMockAdapter. Here is a short example.
First my, very simple, repository: