I have a class that overrides the ToString() method and returns part of a value thats set in the constructor of the class. When attempting to mock this class, JustMock ObjectValue calls the ToString() method before the UserInfo class constructor has run. In this case a null ref exception is thrown.
public string FullName => StringHelper.Format("User: '{0}'.", this.User.FullName);
public UserInfo(User author)
{
this.User = author
}
public override string ToString()
{
return this.FullName;
}
Below is the JustMock code
public ObjectValue(Tuple<Type, object> objectDesc)
{
AssemblyQualifiedName = objectDesc.Item1.AssemblyQualifiedName;
if (objectDesc.Item2 != null)
{
if (objectDesc.Item1.IsValueType)
{
StringValue = string.Format((objectDesc.Item1 == typeof(char)) ? "'{0}'" : "{0}", objectDesc.Item2);
}
else
{
StringValue = string.Format((objectDesc.Item1 == typeof(string)) ? "\"{0}\"" : "{{{0}}}", objectDesc.Item2);
}
}
else
{
StringValue = "<null>";
}
}
I've tried mocking the class but the ToString() method gets called before you can actually mock the method
//Arrange
this.userInfo= Mock.Create<UserInfo>(user);
Mock.Arrange(() => this.userInfo.ToString()).Returns("FullName");
Is there anyway around this?