The following attached test works without enabling the profiler with the runsettings attached to the .csproj.
If we enable it, the DoInstead doesn't run, and the test fails in Visual Studio and vstest console runner.
runsettings:
<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<JUSTMOCK_INSTANCE>1</JUSTMOCK_INSTANCE>
<COR_ENABLE_PROFILING>1</COR_ENABLE_PROFILING>
<COR_PROFILER>{B7ABE522-A68F-44F2-925B-81E7488E9EC0}</COR_PROFILER>
<COR_PROFILER_PATH_64>c:\work\client-dependencies\NuGet\repo\justmock.commercial\2024.4.1203.350\runtimes\win-x64\native\Telerik.CodeWeaver.Profiler.dll</COR_PROFILER_PATH_64>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>
Change COR_ENABLE_PROFILING to 0, and test works.
Production Code:
/// <summary>
/// Pregunta la ruta de la carpeta de exportación.
/// </summary>
/// <param name="exportPDFDafaultPath">Ruta por defecto para exportar PDF.</param>
/// <returns>Ruta de la carpeta de exportación.</returns>
public string AskExportFolderPath(string exportPDFDafaultPath = null)
{
this.systemDialogAdapter.ShowFolderBrowserDialog(Environment.NewLine + "Seleccione una ruta.", exportPDFDafaultPath, out string folderPath, true);
return folderPath;
}
Test Code:
[Fact]
public void AskExportFolderPath_ShouldReturnFolderPath_WhenDialogResultIsOK()
{
// Arrange
var sutAndMocks = GetSutAndMocks();
var exportSharedServiceSut = sutAndMocks.Sut;
string folderPath;
Mock.Arrange(() => sutAndMocks.Mock<ISystemDialogAdapter>().ShowFolderBrowserDialog(Arg.AnyString, Arg.AnyString, out folderPath, Arg.AnyBool, Arg.IsAny<Action<IMessageBoxDialogVM>>(), Arg.IsAny<Action<IMessageBoxDialogVM>>()))
.DoInstead((string descriptionMessage, string startFolderUbicacion, outstring selectedFolder) =>
{
selectedFolder = "test";
});
// Act
var result = exportSharedServiceSut.AskExportFolderPath();
// Assert
Assert.Equal("test", result);
}
private static SutAndMocks<ExportSharedService> GetSutAndMocks()
{
SutAndMocks<ExportSharedService> result = new SutAndMocks<ExportSharedService>();
result.AddMock(Mock.Create<ISystemDialogAdapter>());
result.AddMock(Mock.Create<IFileWrapper>());
result.AddMock(Mock.Create<SaveFileDialog>());
result.AddMock(Mock.Create<BackgroundWorker>());
result.Sut = new ExportSharedService(
result.Mock<ISystemDialogAdapter>(),
result.Mock<IFileWrapper>(),
result.Mock<SaveFileDialog>(),
result.Mock<BackgroundWorker>());
return result;
}
I download last version 2025.1.211.365, same result.
If I mock directly without SutAndMock class works with profiler
Seems profiler change something when we use:
public TMock Mock<TMock>()
{
return (TMock)mocks[typeof(TMock)]; //here the problem (Unboxing)
}
public class SutAndMocks<T>
{
private readonly Dictionary<Type, object> mocks = new Dictionary<Type, object>();
private readonly Dictionary<Type, Dictionary<string, object>> mocksWithName = new Dictionary<Type, Dictionary<string, object>>();
/// <summary>
/// System under test.
/// </summary>
public T Sut { get; set; }
/// <summary>
/// Retorna el Mock de un tipo.
/// </summary>
/// <typeparam name="TMock">Tipo de mock.</typeparam>
public TMock Mock<TMock>()
{
return (TMock)mocks[typeof(TMock)];
}
/// <summary>
/// Retorna el Mock con su name.
/// </summary>
/// <typeparam name="TMock">Tipo de mock con su name</typeparam>
public TMock Mock<TMock>(string name)
{
return (TMock)mocksWithName[typeof(TMock)][name];
}
/// <summary>
/// Añade un mock.
/// </summary>
/// <typeparam name="TMock">Tipo de mock.</typeparam>
public void AddMock<TMock>(TMock mock)
{
this.mocks[typeof(TMock)] = mock;
}
/// <summary>
/// Añade un mock.
/// </summary>
/// <typeparam name="TMock">Tipo de mock.</typeparam>>
/// <param name="name">Nombre de la clase</param>
public void AddMock<TMock>(TMock mock, string name)
{
if (this.mocksWithName.ContainsKey(typeof(TMock)))
{
this.mocksWithName[typeof(TMock)][name] = mock;
}
else
{
this.mocksWithName[typeof(TMock)] = new Dictionary<string, object> { { name, mock } };
}
}
}
Works saving mock into a var, out of arrange delegate:
ISystemDialogAdapter sa = sutAndMocks.Mock<ISystemDialogAdapter>();
Mock.Arrange(() => sa.ShowFolderBrowserDialog(Arg.AnyString, Arg.AnyString, out folderPath, Arg.AnyBool, Arg.IsAny<Action<IMessageBoxDialogVM>>(), Arg.IsAny<Action<IMessageBoxDialogVM>>()))
.DoInstead((string descriptionMessage, string startFolderUbicacion, outstring selectedFolder) =>
{
selectedFolder = "test";
});
Any help? this problem force to change all test of our commercial application, we buy commercial JustMock because need profiler activation for some test, like static class, etc