Skip to main content
Updated and highlighted the description
Source Link
Sumit Bhatia
  • 5.9k
  • 2
  • 9
  • 25

I have the below scenario to implement unit testing on a Sitecore MVCSitecore 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

public class GlassMappingProvider : IMappingProvider { public T GetItem(Guid id, bool isLazy = false, bool inferType = false) where T : class { var context = new SitecoreContext(); return context.GetItem(id, isLazy, inferType); } ... }

  1. All the controllers are being initialized with the wrapper "GlassMappingProvider"

    public class SitecoreTestController : Controller {

         private readonly IMappingProvider _mappingProvider;
    
    
         public SitecoreTestController (IMappingProvider mappingProvider)
         {
             _mappingProvider = mappingProvider;
         }
         ...
     }
    
  2. 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?

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(Guid id, bool isLazy = false, bool inferType = false) where T : class { var context = new SitecoreContext(); return context.GetItem(id, isLazy, inferType); } ... }

  1. All the controllers are being initialized with the wrapper "GlassMappingProvider"

    public class SitecoreTestController : Controller {

         private readonly IMappingProvider _mappingProvider;
    
    
         public SitecoreTestController (IMappingProvider mappingProvider)
         {
             _mappingProvider = mappingProvider;
         }
         ...
     }
    
  2. 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?

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?

Source Link
D J
  • 395
  • 3
  • 11

Implement unit test for a sitecore MVC solution with glassmapper

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(Guid id, bool isLazy = false, bool inferType = false) where T : class { var context = new SitecoreContext(); return context.GetItem(id, isLazy, inferType); } ... }

  1. All the controllers are being initialized with the wrapper "GlassMappingProvider"

    public class SitecoreTestController : Controller {

         private readonly IMappingProvider _mappingProvider;
    
    
         public SitecoreTestController (IMappingProvider mappingProvider)
         {
             _mappingProvider = mappingProvider;
         }
         ...
     }
    
  2. 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?