Advertisement
robincurts

FakeDB Field Vanish

Oct 10th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using FluentAssertions;
  3. using Moq;
  4. using NUnit.Framework;
  5. using Sitecore.Data;
  6. using Sitecore.Data.Items;
  7. using Sitecore.FakeDb;
  8.  
  9. namespace NYL.UnitTests.FakeDB
  10. {
  11.     [TestFixture]
  12.     public class FakeDbFieldValueTest
  13.     {
  14.         private readonly Mock<IRepository> _repo = new Mock<IRepository>();
  15.         private readonly ID _tplId = ID.NewID;
  16.         private readonly ID _fieldId = ID.NewID;
  17.         private readonly string _fieldName = "F";
  18.        
  19.         [SetUp]
  20.         public void SetUp()
  21.         {
  22.             //database = FakeDatabase();
  23.             _repo.Setup(m => m.GetListOfItems()).Returns(Mock_GetListOfItems());
  24.         }
  25.  
  26.         [Test]
  27.         public void Test_FakeDbFieldRetention()
  28.         {
  29.             using (var db = FakeDatabase())
  30.             {
  31.                 // arrange
  32.                 var items = _repo.Object.GetListOfItems();
  33.  
  34.                 // assert
  35.                 items.Should().NotBeNull();
  36.  
  37.                 foreach (var item in items)
  38.                 {
  39.                     // Fields are null after passed back through mock
  40.                     item.Fields[_fieldName].Should().NotBeNull();
  41.                     item[_fieldName].Should().NotBeNullOrEmpty();
  42.                 }
  43.             }
  44.         }
  45.  
  46.         private Db FakeDatabase()
  47.         {
  48.             return new Db
  49.             {
  50.                 new DbTemplate("Child Tpl", _tplId)
  51.                 {
  52.                     new DbField(_fieldName, _fieldId)
  53.                 },
  54.                 new DbItem("Home")
  55.                 {
  56.                     new DbItem("First Child", ID.NewID, _tplId)
  57.                     {
  58.                         {_fieldName, "Value One"}
  59.                     },
  60.                     new DbItem("Second Child", ID.NewID, _tplId)
  61.                     {
  62.                         {_fieldName, "Value Two"}
  63.                     }
  64.                 }
  65.             };
  66.         }
  67.  
  68.         private Item[] Mock_GetListOfItems()
  69.         {
  70.             using (var db = FakeDatabase())
  71.             {
  72.                 var query = string.Format("fast:/sitecore/content/Home//*[@@templateid = '{0}']", _tplId);
  73.                 var items = db.Database.SelectItems(query);
  74.  
  75.                 foreach (var item in items)
  76.                 {
  77.                     // Field Values render fine at creation
  78.                     item.Fields[_fieldName].Should().NotBeNull();
  79.                     item[_fieldName].Should().NotBeNullOrEmpty();
  80.                 }
  81.  
  82.                 return items;
  83.             }
  84.         }
  85.     }
  86.  
  87.     public interface IRepository
  88.     {
  89.         Item[] GetListOfItems();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement