VS2022 - JustMock ThrowElevatedMockingException with .Net 6

2 Answers 153 Views
Code Coverage Integrations JustMock Free Edition Non-public Mocking
Minh
Top achievements
Rank 1
Iron
Minh asked on 18 Apr 2023, 09:12 PM | edited on 20 Apr 2023, 02:30 AM

Hi, 

I am trying to run the unit test for my .net 6 and want to see the code coverage using "Analyse Code Coverage for all Tests" in VS2022.

it successfully run all all test cases when I "run all Tests" but when I use "Analyse Code Coverage for all Tests" to run and see the code coverage, there are some test case failed throwing ThrowElevatedMockingException as below.

 Message: 
Telerik.JustMock.Core.ElevatedMockingException : Cannot mock 'System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[AppHub.SingleView.AmazonFireStickPromo.Models.AccountPromoToken]] GetAccountPromoTokensAsync(System.String)'. The profiler must be enabled to mock, arrange or execute the specified target.
Detected active third-party profilers:
* {324F817A-7420-4E6D-B3C1-143FBED6D855} (from process environment)
Disable the profilers or link them from the JustMock configuration utility. Restart the test runner and, if necessary, Visual Studio after linking.

  Stack Trace: 
ProfilerInterceptor.ThrowElevatedMockingException(MemberInfo member)
MocksRepository.CheckMethodInterceptorAvailable(IMatcher instanceMatcher, MethodBase method)
MocksRepository.AddArrange(IMethodMock methodMock)
MocksRepository.Arrange[TMethodMock](Object instance, MethodBase method, Object[] arguments, Func`1 methodMockFactory)
<>c__DisplayClass24_0`1.<Arrange>b__0()
ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
NonPublicExpectation.Arrange[TReturn](Object target, String memberName, Object[] args)
PromoCodeManagementServiceTests.GetAllEmailStatusesAsync_WithTokenFromAccount_ExpectedListEmailStatus() line 145
GenericAdapter`1.GetResult()
AsyncToSyncAdapter.Await(Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.<Execute>b__0()
DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

I think run all test is successful as I use specify env.runsettings in my project file (attached file). 

but the test run in  "Analyse Code Coverage for all Tests" failed in some methods.

Package I used

    <PackageReference Include="JustMock.Commercial" Version="2023.1.117.1" />

Can someone help me to fix this one ?

Thanks

2 Answers, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 20 Apr 2023, 09:08 AM

Hello Minh,

Inside Visual Studio you need to use the JustMock extension, open the integration tool using the main menu Extensions > JustMock > Intergtaions... and verify whether Visual Studio 2017/2019 Code Coverage/IntelliTrace entry is checked like the one below:

Code Coverage Tools 2

The utility could be also launched outside Visual Studio it is placed in the <JustMockInstallRoot>\Progress\Telerik JustMock\Libraries, but the JustMock extension has to be enabled inside Visual Studio, otherwise the integration with Code Coverage would not succeed. If you are going to use the coverage outside the IDE it is another story that requires different runsettings (you can take a look at the attached template)

I hope the provided information answers your question.

Regards,
Ivo
Progress Telerik

A brand new JustMock course was just added to the Virtual Classroom - the free self-paced technical training portal that gets you up to speed with Telerik and Kendo UI products! Check it out at https://learn.telerik.com.
0
Minh
Top achievements
Rank 1
Iron
answered on 20 Apr 2023, 05:06 PM | edited on 20 Apr 2023, 05:08 PM

Hi Ivo, 

Thanks for reply, I am using VS2022 Enterprise. I couldnt find the Justmock Extension for Vs2022.

but when I run the test via "Analyze Code Coverage for All Tests" with Trace logging Enabled. I found out that the runsettings file using from "Run Test" and "Analyze Code Coverage for All Tests" is different. But I dont know what options cause JustMock fail to mock. 

Test run from "RunTest" settings worked but not from  "Analyze Code Coverage for All Tests"

.runsettings from  "Analyze Code Coverage for All Tests"

<RunSettings>
  <RunConfiguration>
    <EnvironmentVariables>
      <JUSTMOCK_INSTANCE>1</JUSTMOCK_INSTANCE>
      <!-- Profiler settings for .NET Framework -->
      <COR_ENABLE_PROFILING>1</COR_ENABLE_PROFILING>
      <COR_PROFILER>{B7ABE522-A68F-44F2-925B-81E7488E9EC0}</COR_PROFILER>
      <COR_PROFILER_PATH_32>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\32\Telerik.CodeWeaver.Profiler.dll</COR_PROFILER_PATH_32>
      <COR_PROFILER_PATH_64>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\64\Telerik.CodeWeaver.Profiler.dll</COR_PROFILER_PATH_64>
      <!-- Profiler settings for .NET Core and .NET 5 (and up) -->
      <CORECLR_ENABLE_PROFILING>1</CORECLR_ENABLE_PROFILING>
      <CORECLR_PROFILER>{B7ABE522-A68F-44F2-925B-81E7488E9EC0}</CORECLR_PROFILER>
      <!--<CORECLR_PROFILER_PATH_32>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\32\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_32>
			<CORECLR_PROFILER_PATH_64>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\64\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_64>-->
      <CORECLR_PROFILER_PATH_32>C:\Users\TranM\.nuget\packages\justmock.commercial\2023.1.117.1\runtimes\win-x86\native\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_32>
      <CORECLR_PROFILER_PATH_64>C:\Users\TranM\.nuget\packages\justmock.commercial\2023.1.117.1\runtimes\win-x64\native\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_64>
      <CORECLR_ENABLE_PROFILING>1</CORECLR_ENABLE_PROFILING>
      <CORECLR_PROFILER>{324F817A-7420-4E6D-B3C1-143FBED6D855}</CORECLR_PROFILER>
      <CORECLR_PROFILER_PATH_32>C:\Users\TranM\.nuget\packages\microsoft.codecoverage\17.5.0\build\netstandard2.0\InstrumentationEngine\x86\MicrosoftInstrumentationEngine_x86.dll</CORECLR_PROFILER_PATH_32>
      <CORECLR_PROFILER_PATH_64>C:\Users\TranM\.nuget\packages\microsoft.codecoverage\17.5.0\build\netstandard2.0\InstrumentationEngine\x64\MicrosoftInstrumentationEngine_x64.dll</CORECLR_PROFILER_PATH_64>
    </EnvironmentVariables>
    <ResultsDirectory>C:\Users\TranM\Downloads\Projects\AppHub\TestResults</ResultsDirectory>
    <SolutionDirectory>C:\Users\TranM\Downloads\Projects\AppHub\</SolutionDirectory>
    <CollectSourceInformation>False</CollectSourceInformation>
  </RunConfiguration>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector">
        <Configuration>
          <CoverageFileName>TranM_DYE20-LT51-7432 2023-04-20 12_34_30.coverage</CoverageFileName>
          <CodeCoverage>
            <ModulePaths>
              <Exclude>
                <ModulePath>.*CPPUnitTestFramework.*</ModulePath>
              </Exclude>
              <Include>
                <ModulePath>.*\\AppHub\.dll</ModulePath>
                <ModulePath>.*\\AppHubTests\.dll</ModulePath>
              </Include>
            </ModulePaths>
            <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
            <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
            <CollectFromChildProcesses>True</CollectFromChildProcesses>
            <CollectAspDotNet>False</CollectAspDotNet>
            <SymbolSearchPaths />
            <Functions>
              <Exclude>
                <Function>^std::.*</Function>
                <Function>^ATL::.*</Function>
                <Function>.*::__GetTestMethodInfo.*</Function>
                <Function>.*__CxxPureMSILEntry.*</Function>
                <Function>^Microsoft::VisualStudio::CppCodeCoverageFramework::.*</Function>
                <Function>^Microsoft::VisualStudio::CppUnitTestFramework::.*</Function>
              </Exclude>
            </Functions>
            <Attributes>
              <Exclude>
                <Attribute>^System\.Diagnostics\.DebuggerHiddenAttribute$</Attribute>
                <Attribute>^System\.Diagnostics\.DebuggerNonUserCodeAttribute$</Attribute>
                <Attribute>^System\.Diagnostics\.CodeAnalysis\.ExcludeFromCodeCoverageAttribute$</Attribute>
                <Attribute>^Microsoft\.VisualStudio\.TestPlatform\.TestSDKAutoGeneratedCode$</Attribute>
              </Exclude>
            </Attributes>
            <Sources>
              <Exclude>
                <Source>.*\\atlmfc\\.*</Source>
                <Source>.*\\vctools\\.*</Source>
                <Source>.*\\public\\sdk\\.*</Source>
                <Source>.*\\externalapis\\.*</Source>
                <Source>.*\\microsoft sdks\\.*</Source>
                <Source>.*\\vc\\include\\.*</Source>
                <Source>.*\\msclr\\.*</Source>
                <Source>.*\\ucrt\\.*</Source>
              </Exclude>
            </Sources>
            <CompanyNames>
              <Exclude>
                <CompanyName>.*microsoft.*</CompanyName>
              </Exclude>
            </CompanyNames>
            <PublicKeyTokens>
              <Exclude>
                <PublicKeyToken>^B77A5C561934E089$</PublicKeyToken>
                <PublicKeyToken>^B03F5F7F11D50A3A$</PublicKeyToken>
                <PublicKeyToken>^31BF3856AD364E35$</PublicKeyToken>
                <PublicKeyToken>^89845DCD8080CC91$</PublicKeyToken>
                <PublicKeyToken>^71E9BCE111E9429C$</PublicKeyToken>
                <PublicKeyToken>^8F50407C4E9E73B6$</PublicKeyToken>
                <PublicKeyToken>^E361AF139669C375$</PublicKeyToken>
              </Exclude>
            </PublicKeyTokens>
            <EnableStaticNativeInstrumentation>True</EnableStaticNativeInstrumentation>
            <EnableDynamicNativeInstrumentation>False</EnableDynamicNativeInstrumentation>
            <CorrelationId>2e34ea32-a440-4559-8142-c5a4ade80cd8</CorrelationId>
          </CodeCoverage>
          <TelemetryEnabled>True</TelemetryEnabled>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

 

here is .runsettings from "Run Tets"

 <RunConfiguration>
    <EnvironmentVariables>
      <JUSTMOCK_INSTANCE>1</JUSTMOCK_INSTANCE>
      <!-- Profiler settings for .NET Framework -->
      <COR_ENABLE_PROFILING>1</COR_ENABLE_PROFILING>
      <COR_PROFILER>{B7ABE522-A68F-44F2-925B-81E7488E9EC0}</COR_PROFILER>
      <COR_PROFILER_PATH_32>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\32\Telerik.CodeWeaver.Profiler.dll</COR_PROFILER_PATH_32>
      <COR_PROFILER_PATH_64>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\64\Telerik.CodeWeaver.Profiler.dll</COR_PROFILER_PATH_64>
      <!-- Profiler settings for .NET Core and .NET 5 (and up) -->
      <CORECLR_ENABLE_PROFILING>1</CORECLR_ENABLE_PROFILING>
      <CORECLR_PROFILER>{B7ABE522-A68F-44F2-925B-81E7488E9EC0}</CORECLR_PROFILER>
      <!--<CORECLR_PROFILER_PATH_32>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\32\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_32>
			<CORECLR_PROFILER_PATH_64>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\64\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_64>-->
      <CORECLR_PROFILER_PATH_32>C:\Users\TranM\.nuget\packages\justmock.commercial\2023.1.117.1\runtimes\win-x86\native\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_32>
      <CORECLR_PROFILER_PATH_64>C:\Users\TranM\.nuget\packages\justmock.commercial\2023.1.117.1\runtimes\win-x64\native\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_64>
    </EnvironmentVariables>
    <ResultsDirectory>C:\Users\TranM\Downloads\Projects\AppHub\TestResults</ResultsDirectory>
    <SolutionDirectory>C:\Users\TranM\Downloads\Projects\AppHub\</SolutionDirectory>
    <CollectSourceInformation>False</CollectSourceInformation>
  </RunConfiguration>
</RunSettings>
Minh
Top achievements
Rank 1
Iron
commented on 20 Apr 2023, 05:10 PM

Here is my env.runsettings file look like

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
	<RunConfiguration>
		<EnvironmentVariables>
			<JUSTMOCK_INSTANCE>1</JUSTMOCK_INSTANCE>

			<!-- Profiler settings for .NET Framework -->
			<COR_ENABLE_PROFILING>1</COR_ENABLE_PROFILING>
			<COR_PROFILER>{B7ABE522-A68F-44F2-925B-81E7488E9EC0}</COR_PROFILER>
			<COR_PROFILER_PATH_32>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\32\Telerik.CodeWeaver.Profiler.dll</COR_PROFILER_PATH_32>
			<COR_PROFILER_PATH_64>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\64\Telerik.CodeWeaver.Profiler.dll</COR_PROFILER_PATH_64>

			<!-- Profiler settings for .NET Core and .NET 5 (and up) -->
			<CORECLR_ENABLE_PROFILING>1</CORECLR_ENABLE_PROFILING>
			<CORECLR_PROFILER>{B7ABE522-A68F-44F2-925B-81E7488E9EC0}</CORECLR_PROFILER>
			<!--<CORECLR_PROFILER_PATH_32>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\32\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_32>
			<CORECLR_PROFILER_PATH_64>C:\Program Files (x86)\Progress\Telerik JustMock\Libraries\CodeWeaver\64\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_64>-->
			<CORECLR_PROFILER_PATH_32>C:\Users\TranM\.nuget\packages\justmock.commercial\2023.1.117.1\runtimes\win-x86\native\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_32>
			<CORECLR_PROFILER_PATH_64>C:\Users\TranM\.nuget\packages\justmock.commercial\2023.1.117.1\runtimes\win-x64\native\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH_64>

		</EnvironmentVariables>
	</RunConfiguration>
</RunSettings>

Ivo
Telerik team
commented on 24 Apr 2023, 09:01 AM

The differences above are no surprise to me, that is how things work. The JustMock extension for Visual Studio is part of the product installation, available in your Telerik account. There is a way to configure code coverage via a NuGet package, but it is good to know if there is anything preventing you from using the extension.
Minh
Top achievements
Rank 1
Iron
commented on 25 Apr 2023, 06:09 PM

I am currently use coverlet to get the coverage instead. seems to me that Justmock not working with Microsoft code coverage collector. Is there any way to config Justmock to work with Microsoft collector ? I tried with the coverage zip file you provided but it not worked.
Ivo
Telerik team
commented on 26 Apr 2023, 02:37 PM

Please find attached a basic sample that uses Microsoft Code Coverage as a NuGet package on the command line. If you need to run the tests with coverage inside Visual Studio you have to install the JustMock extension.
Tags
Code Coverage Integrations JustMock Free Edition Non-public Mocking
Asked by
Minh
Top achievements
Rank 1
Iron
Answers by
Ivo
Telerik team
Minh
Top achievements
Rank 1
Iron
Share this question
or