Testing a Service

Now that we are familiar with Stubbing Data and the Mock Database Service, we can put those together to write some tests. We will write tests for a few of the examples from the service we wrote earlier. The steps for writing a test are:
  1. Stub the data required
  2. Instantiate the MockDatabaseService instance
  3. Instantiate the service being tested
  4. Execute the method being tested
  5. Assert the results of the method
  6. Assert the database access

namespace DocExample.Tests
{
    using DatabaseAbstraction;
    using DatabaseAbstraction.Utils;
    using DocExample.Models;
    using DocExample.Queries;
    using DocExample.Services;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Unit tests for the ExampleService class
    /// </summary>
    [TestClass]
    public class ExampleServiceTest
    {
        /// <summary>
        /// Test the positive path of the GetExample() method
        /// </summary>
        [TestMethod]
        public void ExampleService_GetExample_Success()
        {
            var exampleData = new StubDataReader("id");
            exampleData.AddRow("78");

            using (var db = new MockDatabaseService(new StubDataReader(exampleData),
                    typeof(ExampleQueryProvider)))
            {
                var service = new ExampleService(db);

                var theExample = service.GetExample(78);

                Assert.IsNotNull(theExample,
                    "The example should not have been null");
                Assert.AreEqual(78, theExample.ID,
                    "The example was not filled correctly");

                db.AssertPeformedSelect("example.select", 1,
                    DbUtils.SingleParameter("example_id", 78));
            }
        }

        /// <summary>
        /// Test the negative path of the GetExample() method
        /// </summary>
        [TestMethod]
        public void ExampleService_GetExample_NotFound()
        {
            var exampleData = new StubDataReader("id");

            using (var db = new MockDatabaseService(new StubDataReader(exampleData),
                    typeof(ExampleQueryProvider)))
            {
                var service = new ExampleService(db);

                var theExample = service.GetExample(82);

                Assert.IsNull(theExample,
                    "The example should have been null");

                db.AssertPeformedSelect("example.select", 1,
                    DbUtils.SingleParameter("example_id", 82));
            }
        }
    }
}
TIP: As the list of query and fragment types grows larger, it may be helpful to create a helper method in the unit test, so that you only need to maintain one list of types required to create the queries your test will be executing.

Testing asynchronous services is a little different, although the concept is the same. Below are similar tests for the asynchronous version.
namespace DocExample.Tests
{
    using DatabaseAbstraction;
    using DatabaseAbstraction.Async;
    using DatabaseAbstraction.Utils;
    using DocExample.Models;
    using DocExample.Queries;
    using DocExample.Services;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Threading.Tasks;

    /// <summary>
    /// Unit tests for the asynchronous ExampleService class
    /// </summary>
    [TestClass]
    public class ExampleServiceTest
    {
        /// <summary>
        /// Test the positive path of the GetExample() method
        /// </summary>
        [TestMethod]
        public asyc Task ExampleService_GetExample_Success()
        {
            var exampleData = new StubDataReader("id");
            exampleData.AddRow("78");

            var db = new MockDatabaseService(new StubDataReader(exampleData),
                typeof(ExampleQueryProvider));
            using (var data = new DatabaseServiceAsync(db))
            {
                var service = new ExampleService(data);

                var theExample = await service.GetExample(78);

                Assert.IsNotNull(theExample,
                    "The example should not have been null");
                Assert.AreEqual(78, theExample.ID,
                    "The example was not filled correctly");

                db.AssertPeformedSelect("example.select", 1,
                    DbUtils.SingleParameter("example_id", 78));
            }
        }

        /// <summary>
        /// Test the negative path of the GetExample() method
        /// </summary>
        [TestMethod]
        public async Task ExampleService_GetExample_NotFound()
        {
            var exampleData = new StubDataReader("id");

            var db = new MockDatabaseService(new StubDataReader(exampleData),
                typeof(ExampleQueryProvider));
            using (var data = new DatabaseServiceAsync(db))
            {
                var service = new ExampleService(data);

                var theExample = await service.GetExample(82);

                Assert.IsNull(theExample,
                    "The example should have been null");

                db.AssertPeformedSelect("example.select", 1,
                    DbUtils.SingleParameter("example_id", 82));
            }
        }
    }
}

<<

Last edited Apr 2, 2013 at 2:38 AM by danielsummers, version 6

Comments

No comments yet.