SaaS CMS has officially launched! Learn more now.

Niklas Melinder
Nov 29, 2010
  6456
(3 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

John Parr
John Parr Nov 29, 2010 09:21 AM

'Hassle' often means I can't do it easily, quickly and cheaply as I need to or would like.

The whole 'how much effort do we test mentality' can be both political, economic and ideological and is often decided and imposed on us by others.

So let's not blame EPiServer because the whole point of testing the view or presentation in web forms it a whole topic by itself.

Joel Abrahamsson
Joel Abrahamsson Nov 29, 2010 10:13 AM

John, I think Niklas was referring to the EPiServer API. And when it comes to that I actually think we should blame EPiServer :-)
After all it is their API that forces us to either rely on concrete implementations instead of abstractions and thereby violating a bunch of design principles or to rely on third party abstractions of their API, ie EPiAbstractions.
Luckily they are working to fix a lot of those problems in the next major version by creating IDataFactory, (hopefully) making members of PageData virtual etc. And they should definitely be applauded for having made the DDS well designed and testable (kudos Paul!).

Niklas Melinder
Niklas Melinder Nov 29, 2010 12:27 PM

Testing code using the EPiServer API is a hassle. Testing code using WebForms is to, but that is a whole other story.

The point of this post was to show how easy it can be to test code when the framework you are using supports it.

smithsson68@gmail.com
smithsson68@gmail.com Dec 1, 2010 02:50 PM

Thanks Joel for the kudos but obviously DDS was a team effort :-)

Matthew Howard
Matthew Howard Sep 4, 2012 03:24 PM

How do you delete an item from a dynamicdatastore and edit an existing item?

Please login to comment.
Latest blogs
Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024