I’m working on Unit test cases and mocking a reference field could be a complex task.

Here is the code, which may help you

[Fact]
public void ReferenceFieldMockingTest()
{
ID newTemplateId = ID.NewID;
ID referenceItemId = ID.NewID;
ID referenceItemId1 = ID.NewID;
using (Db db = new Db
{
new DbTemplate(“ItemTemplate”, templateId) { “FieldName” },
new DbItem(“ReferenceItem”, referenceItemId),
new DbItem(“ReferenceItem1”, referenceItemId1),
new DbItem(“Home”)
{
new DbItem(“New Item”)
{
new DbItem(“Test”, ID.NewID, templateId)
{

new DbField(“FieldName”)
{
Value=referenceItemId.ToString(),
Type =”Lookup”,
}
},
new DbItem(“ABC”, ID.NewID, templateId)
{
new DbField(“FieldName”)
{
Value=referenceItemId1.ToString(),
Type =”Lookup”,
}
},
}
})

 

Hope this helps!


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Fake DB

How to do unit testing for media items in Sitecore + xUnit testing : Fake DB

Unit testing in Sitecore is a tricky one since you need create an items in Fake dB. I didn’t find much information about Media items unit testing and below piece of code may help you! Read more…

Fake DB

PageContext item in Sitecore Fake DB + Unit test cases

I tried to set PageContext using  RenderingContext.EnterContext but didn’t work using (RenderingContext.EnterContext(new Rendering(), contextItem)) { I have set it using  ContextService.Get().Push(pageContext); Here is the code, which may help you using (Db db = new Db using (Db Read more…

Sitecore

Sitecore 8 Experience Editor: A serious error has occurred please contact the administrator

In Sitecore version 8.0 rev. 150621 I am experiencing issue when clicking on Experience Editor or Preview button in the Publish tab. The experience editor opens with the “A serious error has occurred please contact Read more…