World is now on Opti ID! Learn more


Nov 29, 2010
  4918
(0 votes)

Testable code using Dynamic Data Store

There is some talk about how testing using EPiServer is a hassle. And it is for many reasons. There are exceptions though, and Dynamic Data Store is one of them. In contrast to EPiServer.DataFactory for example, which is a sealed class, both DynamicDataStoreFactory and DynamicDataStore are abstract classes. This is good news from a testing perspective since we can override their behavior. Good work team!

The example I´m gonna show is from the EPiSocial plugin I´ve been working on, which uses DDS. This is from the CommentRepository.

Setup

First some setup changes. Instead of using DynamicDataStoreFactory.Instance to get the store:

   1: public DynamicDataStoreCommentRepository()
   2: {
   3:     this.commentStore = DynamicDataStoreFactory.Instance.GetStore(typeof(PageComment));
   4: }

We can instead take a dependency to the abstract class DynamicDataStoreFactory:

   1: public DynamicDataStoreCommentRepository(DynamicDataStoreFactory dynamicDataStoreFactory)
   2: {
   3:     this.commentStore = dynamicDataStoreFactory.GetStore(typeof(PageComment));
   4: }

With this small change we´ll be able to test the code in the CommentRepository.

Let´s use the GetLatestComments method as the scenario for this:

   1: public IEnumerable<PageComment> GetLatestComments(int count, string pageLanguage)
   2: {
   3:     return this.commentStore.Items<PageComment>()
   4:         .Where(c => c.PageLanguage == pageLanguage)
   5:         .OrderByDescending(c => c.Created)                
   6:         .Take(count);
   7: }

Testing

To test that method we have to setup a fake data store to test against. I did this using Moq. Let´s say that we want to test that the method returns only pages in the language that we provide. It could look something like this:

   1: [Test]
   2: public void Given_a_store_with_comments_for_multiple_pages_when_getting_latest_comments_then_only_comments_for_provided_page_language_is_returned()
   3: {
   4:     // Arrange
   5:     var pageCommentList = new List<PageComment>();
   6:     pageCommentList.Add(new PageComment { PageId = 1, PageLanguage = "sv" });            
   7:     pageCommentList.Add(new PageComment { PageId = 2, PageLanguage = "en" });
   8:  
   9:     IOrderedQueryable<PageComment> queryableItemList = new EnumerableQuery<PageComment>(pageCommentList );
  10:     var dataStoreFactoryMock = new Mock<DynamicDataStoreFactory>();
  11:     var dataStoreMock = new Mock<DynamicDataStore>(null);
  12:  
  13:     dataStoreFactoryMock
  14:         .Setup(x => x.GetStore(typeof(PageComment)))
  15:         .Returns(dataStoreMock.Object);
  16:  
  17:     dataStoreMock
  18:          .Setup(x => x.Items<PageComment>())
  19:          .Returns(queryableItemList);
  20:  
  21:     var ddsCommentRepository = new DynamicDataStoreCommentRepository(dataStoreFactoryMock.Object);
  22:  
  23:     // Act
  24:     var latestComments = ddsCommentRepository.GetLatestComments(3, "sv");
  25:  
  26:     // Assert
  27:     Assert.That(latestComments, Contains.Item(pageCommentList[0]));
  28:     Assert.That(latestComments, !Contains.Item(pageCommentList[1]));      
  29: }

That´s pretty much it, not that hard really. I think this shows how easy testing can be if it is somewhat supported by the framework. Hoping for more of this in upcoming EPiServer versions.

Nov 29, 2010

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |