Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jeremy Davis
Jeremy Davis

Posted on • Originally published atblog.jermdavis.dev on

     

Unit testing Sitecore computed fields

Quick one today. I was writing some code for Sitecore Computed Index fields recently, and it took me more Google Effort than I felt it should have to work out how to write unit tests with FakeDB to verify the code worked. If you want to do that without spending a while searching, the answer is pleasingly simple:

When you declare a computed field, the key method you’re implementing looks like this:

publicoverrideobjectComputeFieldValue(IIndexableindexable){}
Enter fullscreen modeExit fullscreen mode

But when you implement a test using FakeDb you’re dealing withItem objects:

[TestMethod]publicvoidClassUnderTest_Description_OfTheTestCase(){using(vardb=ComputedFieldsFakeDb.Create()){varitm=Sitecore.Context.Database.GetItem("/sitecore/content/TheItemToIndex");varsut=newComputedFieldUnderTest();varresult=sut.ComputeFieldValue(/* what goes here to do the mapping? */);Assert.AreEqual("somevalue",result);}}
Enter fullscreen modeExit fullscreen mode

So what do you do to map theItem into anIIndexable?

Well, turns out it’s pretty trivial – There’s a type calledSitecoreIndexableItem which wraps anItem to give you anIIndexable:

varresult=sut.ComputeFieldValue(newSitecore.ContentSearch.SitecoreIndexableItem(itm));
Enter fullscreen modeExit fullscreen mode

Simple.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Herder of cats: Professionally as a Sitecore MVP and architect for @unrvldagency. Parentally as a father. Literally as a cat owner.
  • Location
    Leeds, UK
  • Work
    Solution Architect at UNRVLD
  • Joined

More fromJeremy Davis

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp