Please follow the below-mentioned steps and try to incorporate them with your existing code:
- 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;
- Define the variables in your Test Class:
private Mock<IMvcContext> mvcContextMock;
private Mock<HeroController> controller;
- 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 };
}
- 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.