JustMock calling ToString() on class that overrides ToString(), Throws null ref

1 Answer 35 Views
General Discussions
Keenan
Top achievements
Rank 1
Keenan asked on 04 Apr 2024, 05:17 PM

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?

1 Answer, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 08 Apr 2024, 01:27 PM

Hello Keenan,

I have made a minimal sample project (see attached) using the provided code and some assumptions, but I could not observe such an issue, probably I do not fully understand the idea behind. Could you please modify the project so it could reproduce the problem. Once we have a reproducible scenario we can start looking at it for potential issues.

Regards,
Ivo
Progress Telerik

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com
Keenan
Top achievements
Rank 1
commented on 09 Apr 2024, 04:37 PM | edited

Hey Ivo,

Sorry I missed an important part and replicated it with your solution. I added another class that UserInfo Implements

namespace MyClassLibrary
{
    public abstract class DbEntity
    {
        public abstract string HeldName { get; }
    }
}

namespace MyClassLibrary
{
    public class UserInfo : DbEntity
    {
        public string FullName => StringHelper.Format("User: '{0}'.", this.User.FullName);

        public override string HeldName => "user-info";

        public readonly User User;

        public UserInfo(User author)
        {
            this.User = author;
        }

        public override string ToString()
        {
            return this.FullName;
        }
    }
}




Ivo
Telerik team
commented on 11 Apr 2024, 03:18 PM

Hello Keenan, I am still unable to figure out what the method ObjectValue is and how it fits in the whole picture, please provide a sample code.
Keenan
Top achievements
Rank 1
commented on 11 Apr 2024, 04:03 PM

Ivo,

ObjectValue is a Telerik class in the call stack that is calling ToString() on the base class.  Run the attached solution and view the stack trace. 

Ivo
Telerik team
commented on 12 Apr 2024, 06:35 AM

Hello Keenan, it is clear now, I had to check the JustMock code first instead of asking dumb questions, anyway. The issue is related to the DebugWindow feature, so let us try disabling it (on the picture below) and see whether it fixes the problem.

Keenan
Top achievements
Rank 1
commented on 12 Apr 2024, 04:48 PM

Hey Ivo,

 

That appears to prevent the issue. Is there anyway around this without disabling it? Not sure what functionality we'll be losing disabling it. 

Ivo
Telerik team
commented on 17 Apr 2024, 08:14 AM

Hello Keenan, DebugWindow allows getting a closer look at what is going on inside the framework and it is completely safe to disable it without losing anything from the core functionality. Regarding the possibility of workaround in another way I am afraid there is no simple way to do so.
Tags
General Discussions
Asked by
Keenan
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Share this question
or