'Unable to mock a static property of a single ton class
ObjectData which contains the singleton property:
public class ObjectData
{
public static IObjectData singleTon
{
get
{
// code for singleton
return new ObjectData();
}
}
public virtual T CreateObject<t>()
{
return //single object of T ;
}
}
Singleton helper which sends a type for creating an object:
public static class SingletonHelper
{
public static Model myModel
{
get { return ObjectData.singleTon.CreateObject<Model>(); }
}
}
// Model which contain a property of folderName
public class Model {
public string folderName {get;set;}
}
// pathCalculate for which I am going to write unit test
public class pathCalculate {
public string myFolderName
{
get { return SingletonHelper.myModel.folderName }
}
}
// I have tried
// Assign
Mock<IObjectData> objectData = new Mock<IObjectData>();
Mock<Model> model = new Mock<Model>();
model.Setup(m => m.folderName).Returns("my expected folder Name");
objectData.Setup(m => m.CreateObject<Model>()).Returns(model.Object);
//Act
pathCalculate cl = new pathCalculate();
//Assert
Assert.AreEqual("my expected folder Name", cl.myFolderName);
In the Assert part, I get an error
myFolderName** value Null
I believe using this code there is some problem while mocking the CreateObject function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
