Skip to main content
deleted 8 characters in body
Source Link

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<ClearingHeroController>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.

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<ClearingHeroController>(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.

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.

Source Link

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<ClearingHeroController>(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.