0

I have the below scenario to implement unit testing on a Sitecore MVC (with glassmapper) integration.

  1. We are using a wrapper class to make calls to Glassmapper Sitecore Context methods. Below is one of the example.

     public interface IMappingProvider
     {
          T GetItem<T>(Guid id, bool isLazy = false, bool inferType = false) where T : class;
         ...
     }
    
     public class GlassMappingProvider : IMappingProvider
     {
          public T GetItem<T>(Guid id, bool isLazy = false, bool inferType = false) where T : class
          {
               var context = new SitecoreContext();
               return context.GetItem<T>(id, isLazy, inferType);
          }
          ...
     }  
    
  2. All the controllers are being initialized with the wrapper GlassMappingProvider

     public class SitecoreTestController : Controller
     {
         private readonly IMappingProvider _mappingProvider;
    
         public SitecoreTestController (IMappingProvider mappingProvider)
         {
             _mappingProvider = mappingProvider;
         }
         ...
     }
    
  3. I have followed multiple articles where in to create Mock Sitecore Context, create Sitecore Fake DB, create the Item and set in the fake context. The challenge I have are bwlow

  • Could not pass the Sitecore Context to controllers
  • Could not Mock the GetItem of the Glass Wrapper - GlassMappingProvider.

Any one had the similar implementation or any suggestions on what to be done to write a test cases for such a implementation?

1 Answer 1

1

Please follow the below-mentioned steps and try to incorporate them with your existing code:

  1. Create a Test controller in your project, for example, I am creating HeroTestController and specifying the namespace of your controller:
using HeroController = global::YourProjectName.HeroController;
  1. Define the variables in your Test Class:
private Mock<IMvcContext> mvcContextMock;
private Mock<HeroController> controller;
  1. Initialize those variables into your TestInitialize Method:
[TestInitialize]
public void TestInitialize()
 {
   this.mvcContextMock= new Mock<IMvcContext>();
   this.controller = new Mock<HeroController>(this.mvcContextMock.Object) { CallBase = true };
 }
  1. Write your Test method:
[TestMethod]
public void TestMethodA()
 {
   // arrange 
   GetItemByIdOptions option = new GetItemByIdOptions(this.YourObject.Id) { InferType = false, Lazy = Glass.Mapper.LazyLoading.Disabled };
   this.mvcContextMock.Setup(x => x.SitecoreService.GetItem<ModelName>(option))
                .Returns(this.YourObject);

  // act
           
  // assert 
           
}

Steps 1,2 and 3 can solve your first problem i.e. Could not pass the Sitecore Context to controllers.

Steps 4 can solve your second problem i.e. Could not Mock the GetItem of the Glass Wrapper - GlassMappingProvider.

Hope it helps you.

1
  • Thanks for the detailed explanation Himmat. I understand that this approach will help creating test cases for any new controller where Sitecore Context is being used. But I would need a solution where the existing controllers code can be untouched and implement Test cases as well. Once again, Thanks for taking the time and providing a detailed solution with code example
    – D J
    May 23, 2022 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.