Tuesday, March 8, 2016

Fake Entire EpiServer

Intro
When doing the integration testing in EpiServer, you will need to mock more than one or two methods or services. Sometimes you need to implement the same ground for different test classes. but it will be very time consuming and at the same time, very hard to write.
Take a simple collaboration service as an example. Your service probably used 3-4 different EpiServer services and 3-4 of your other services (at least:) ), but when you want to test them you find out that you have to mock all used services for your class/service.

An alternative to this option will be to fake all APIs that you've used from EpiServer and then load the faked content on the initial step of your test classes.

Well, it is like having a huge castle floating in the air. :) time consumable to implement but really easy to expand and change.

FakeMaker
So I have an advice for you if you want to fake EpiServer content. Use the FakeMaker by David Vujic to create your pages which will add them automatically to your repository and serviceLocator.
You can more data about it here or download it from github or even nuget.

Need to mention 2 things:
1- Don't do create a new serviceLocator. Create your pages with FakeMaker and then get your ServiceLocator mock object using this command:
Code:
 var mockLocator = Mock.Get<IServiceLocator>(ServiceLocator.Current);
You will have access to the content repository with your FakeMaker.ContentRepository easily.

2- If you have EpiServer 8 you cannot use the nuget. just download his code from github and use it. It is very well written and easy to use

My Extention
When I saw FakeMaker, I understand that it can be pretty usable for many people including me :)  so I had a discussion with David and he liked my Ideas. Then I realized that I can contribute in the project so I add other features to my branch. It is not that big deal, but it can be useful for many.
I've add 2 options:
1-Supporting EntryContentBase for Commerce system
You can now easily add your products to your fake. It is as easy as creating FakePages. For instance:
Code:
 var shopRoot = FakePage.Create("ShopRoot");
 var fakePhone = FakeEntryContent.Create<PhoneProduct>("Iphone 6").ChildOf(shopRoot);
_fake.AddToRepository(shopRoot);

2-Supporting ContentLoader
In our code, we used ContentLoader in many places while we also used ContentRepository. Of course I wanted to support the ContentLoader when I call AddToRepository :)
You can have access to ContentLoader the same as ContentRepository by FakeMaker.ContentLoader.
Just to mention, it make more sense to create your content in FakeMaker first and then use ContentLoader, ContentRepository, and serviceLocator that it creates for you.


*You can find my code in here. Just to mention, it is not confirmed by David yet, so it is only on EpiServer 8 that I am using right now and it is not available through nuget pakages, yet.


No comments:

Post a Comment