Skip to main content
Home  ›  Blog

Tutorial Custom DataSources #3 Unit Testing

DataSource objects are ideal for unit testing. Here's how to do it.

This is part of the series on custom data sources. If you haven't read the other ones yet, I recommend you check those out first:

  1. Part 1: The most basic DataSource delivering 1 item
  2. Part 2: Creating lists which come from elsewhere
  3. Part 3: Unit Testing DataSources
  4. Part 4: Creating a distributable DNN package with your DataSource
  5. Part 5: Giving the admin a configuration UI

Let's start Testing!

DataSource objects are ideal for testing, because:

  1. They can have clearly defined input and output
  2. They work in an isolated way
  3. Usually at runtime you don't see them, to testing them manually takes a lot of work

Because of this, all our standard datasources have many tests to increase quality and reliability. For our Tutorial #1 and #2 we created some basic tests for demo. You'll find the test-project as part of the git-solution here. So here's an example of test-code which tests the List solution in the second part of this tutorial:

using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ToSic.Tutorial.DataSource;

namespace ToSic.Tutorial.Datasource.Tests
{
    [TestClass]
    public class TestDateTime_BasicList
    {
        [TestMethod]
        public void DateTimeDataSource_HasManyItems()
        {
            var dtmDs = new DateTimeDataSourceBasicList();
            Assert.AreEqual(DateTimeDataSourceBasicList.ItemsToGenerate, dtmDs.List.Count(), $"make sure it has exactly {DateTimeDataSourceBasicList.ItemsToGenerate} item in the list");

            var item = dtmDs.List.First();
            Assert.IsNotNull(item, "the item must be a real object");
        }

        [TestMethod]
        public void DateTimeDataSource_HasTwoAttributes()
        {
            var dtmDs = new DateTimeDataSourceBasicList();
            var item = dtmDs.List.First();
            Assert.AreEqual(2, item.Attributes.Count, "has only 2 property");
        }


        [TestMethod]
        public void DateTimeDataSource_HasDateFieldWhichIsDate()
        {
            var dtmDs = new DateTimeDataSourceBasicList();
            var item = dtmDs.List.First();
            var dateAsObject = item.GetBestValue(DateTimeDataSourceBasicList.DateFieldName);
            Assert.IsNotNull(dateAsObject);

            var dateAsDate = dateAsObject as DateTime?;
            Assert.IsNotNull(dateAsDate);

        }
    }
}

As you can see, we have 3 trivial tests here:

  1. Check that it works and returns exactly 7 items
  2. Check that the first item has 2 attributes
  3. Check that the relevant field we care about is a date

This is just an example. You'll also find many more examples in the EAV test projects

Hope this helps :)
Daniel


Daniel Mettler grew up in the jungles of Indonesia and is founder and CEO of 2sic internet solutions in Switzerland and Liechtenstein, an 20-head web specialist with over 800 DNN projects since 1999. He is also chief architect of 2sxc (see github), an open source module for creating attractive content and DNN Apps.

Read more posts by Daniel Mettler