This is a migrated thread and some comments may be shown as answers.

.NET Core MSTest Project throwing method not found error

10 Answers 3858 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 06 Mar 2019, 05:31 PM

I am trying to create a new .NET Core project and I am having issues using any JustMock functions/keywords. I have installed the latest available installer (JustMock.2019.1.207.1), we use JustMock in our other projects without any issues but these are all .NET 4.5. I would prefer to use the MSTest project as that is what the other projects use also and would like to keep things consistent. I have also tried creating a new NUnit and XUnit project but keep getting the same error. Solution builds without errors or warnings.

 

My Test Method only has one line in it for arranging, it's trying to mock the service. Here is the code:

var context = Mock.Create<IFileService>();

Here is the error I am getting:
Test method MSTestProject.LayoutControllerTest.FileController_TestCalls threw exception: 
System.TypeInitializationException: The type initializer for 'Telerik.JustMock.Core.ProfilerInterceptor' threw an exception. ---> System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.

StackTrace: 

at Telerik.JustMock.Core.Castle.DynamicProxy.ModuleScope.CreateModule(Boolean signStrongName)
   at Telerik.JustMock.Core.Castle.DynamicProxy.ModuleScope.ObtainDynamicModuleWithStrongName()
   at Telerik.JustMock.Core.MockingUtil.get_ModuleBuilder()
   at Telerik.JustMock.Core.MockingUtil.CreateDynamicMethodWithVisibilityChecks(Type returnType, Type[] parameterTypes, Action`1 ilGen)
   at Telerik.JustMock.Core.ProfilerInterceptor.CreateFieldAssignmentExpression(FieldInfo assignee, ParameterExpression valueParam)
   at Telerik.JustMock.Core.ProfilerInterceptor.InitializeFieldAccessors[TFieldType](String fieldName, Func`1& getter, Action`1& setter)
   at Telerik.JustMock.Core.ProfilerInterceptor..cctor()
--- End of inner exception stack trace ---
    at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal(Action guardedAction)
   at MSTestProject.LayoutControllerTest.FileController_TestCalls() in C:\Repos\LTA-TelerikUIDotNetCore\MSTestProject\LayoutControllerTest.cs:line 33

 

10 Answers, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 07 Mar 2019, 04:50 PM
Hello Josh,

When you create a new test project targeting .net core for executing tests with JustMock you should add the System.Security.Permissions. Here is a link to the NuGet package.

After adding the package the test should pass successfully.

Regards,
Mihail
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Josh
Top achievements
Rank 1
answered on 08 Mar 2019, 01:24 PM
I tried this before posting. Tried again after creating a brand new Core solution and redownloading the JustMock installer  and I still get the same error. 
0
Mihail
Telerik team
answered on 11 Mar 2019, 11:37 AM
Hi Josh,

This is quite strange.
I have attached a sample project where the tests pass successfully. You could also check the attached screenshot showing the result.
Could you please test with this sample project and get back to us with your result?


Regards,
Mihail
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Josh
Top achievements
Rank 1
answered on 12 Mar 2019, 09:18 PM

We have finally got our tests to work. I could not get your solution to work or build, There were too many broken references and the NuGet packages were not loading correctly for some reason. And after removing and re-adding those same versions, I still receivedthe same error.

What did finally work was including the NuGet package for JustMock. We had the assembly version, that come from the MSI installer, included in our solution with the same version. Another team member who was also looking into this added the NuGet package on a whim of the same version (2019.1.207.1). This worked and now we do not have any issues running test. 

 

Any ideas as to why the NuGet package would have worked when the DLL did not?

0
Mihail
Telerik team
answered on 13 Mar 2019, 07:46 AM
Hello Josh,

Please have in mind that the JustMock NuGet version from NuGet.org is the free version (JustMock Lite) which does not have the same set of functionality as the commercial version JustMock. Meaning that if you want to take advantage of the elevated mocking functionality you won't be able to do so with your current setup.

Regarding the broken references, this is expected as the archive does not contain any assemblies. One of the main reasons for this is because the project uses packages references and the packages are installed in a global store at your PC user account.

Could you please make sure that the settings of your VS 2017 related to NuGet packages are the same as described in the Package Restore article. I assume that you use the latest version of VS 2017, if this is not the case, please provide information on what is your VS version.

When I have problems with some NuGet packages that are not restored by building the solution in VS I always try to restore them with nuget.exe calling it from a command line. To do so you will need to download the latest stable version of nuget.exe from here and perform the "nuget.exe restore EFTestProject.sln". This command will be valid if the nuget.exe is located where the EFTestProject.sln is and the console active directory is moved at the same location. Attached you can find the result of the command.

I would appreciate if you could share the result of restoring the NuGet packages with the command line.

Regards,
Mihail
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Josh
Top achievements
Rank 1
answered on 14 Mar 2019, 10:26 PM

Good to know, we will need the full version as we use a lot of the other functionality it offers. But it still begs the question of why the NuGet package seemed to fix the issue of the test passing when the DLL did not.

I was able to finally get the restore done (I think) and attached the screenshot of the output, but I am still getting the same error. I am using the latest version of VS 2017 (15.9.6). 

I have also tried uninstalling and reinstalling JustMock. Still get the same error message. 

I have asked others to also try and they are also getting the same error. Are there system requirements for running this version of JustMock? I wouldn't think this would be a factor but stranger things have been an issue before. 

0
Accepted
Mihail
Telerik team
answered on 15 Mar 2019, 09:22 AM
Hello Josh,

I finally managed to reproduce the described exception.

Result StackTrace:
at Telerik.JustMock.Core.Castle.DynamicProxy.ModuleScope.CreateModule(Boolean signStrongName)
   at Telerik.JustMock.Core.Castle.DynamicProxy.ModuleScope.ObtainDynamicModuleWithStrongName()
   at Telerik.JustMock.Core.MockingUtil.get_ModuleBuilder()
   at Telerik.JustMock.Core.MockingUtil.CreateDynamicMethodWithVisibilityChecks(Type returnType, Type[] parameterTypes, Action`1 ilGen)
   at Telerik.JustMock.Core.ProfilerInterceptor.CreateFieldAssignmentExpression(FieldInfo assignee, ParameterExpression valueParam)
   at Telerik.JustMock.Core.ProfilerInterceptor.InitializeFieldAccessors[TFieldType](String fieldName, Func`1& getter, Action`1& setter)
   at Telerik.JustMock.Core.ProfilerInterceptor..cctor()
--- End of inner exception stack trace ---
    at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal(Action guardedAction)
   at EFTestProject.UnitTest.TestMethod() in C:\Users\vladov\Desktop\Support\EFTestProject\EFTestProject\UnitTest.cs:line 13
Result Message:
Test method EFTestProject.UnitTest.TestMethod threw exception: 
System.TypeInitializationException: The type initializer for 'Telerik.JustMock.Core.ProfilerInterceptor' threw an exception. ---> System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.


Steps to reproduce the problem:
1. Create Unit test project targeting .Net Core.
2. Add a reference to the Telerik.JustMock.dll that targets the .Net Framework.
3. Create at least one mocked object in a test method.
4. The project will be built without a problem in Visual Studio.
5. Make sure that the JustMock profiler is enabled from the JustMock Visual Studio extension.
6. Run the test containing the mocked object.

Result: System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'

The problem should be fixed by adding a reference to the Telerik.JustMock.dll that targets .Net Core. The location of this assembly is at <JustMockInstallationPath>\Libraries\netcoreapp2.0\Telerik.JustMock.dll. Where typically the JustMockInstallationPath is C:\Program Files (x86)\Progress\Telerik JustMock


I hope this information is helpful.

Regards,
Mihail
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Josh
Top achievements
Rank 1
answered on 18 Mar 2019, 01:12 PM
This fixed the issue. Thank you! I can't believe I missed that.... 
0
Indrayan
Top achievements
Rank 1
answered on 19 Nov 2019, 10:03 AM

The problem should be fixed by adding a reference to the Telerik.JustMock.dll that targets .Net Core. The location of this assembly is at <JustMockInstallationPath>\Libraries\netcoreapp2.0\Telerik.JustMock.dll. Where typically the JustMockInstallationPath is C:\Program Files (x86)\Progress\Telerik JustMock

 

This is something that should be explicitly mentioned in documentation. I should not have had to waste hours before stumbling here to find the solution for making even one line of JustMock work in a .Net Core solution.

0
Ivo
Telerik team
answered on 19 Nov 2019, 12:00 PM

Hello Indrayan,

JustMock R2.2019 release delivers the fix for assembly reference issue in the project templates for .NET Core. In case of migration an existing project to .NET Core, it requires some manual steps. I agree with you, that it might be a bit unclear and as you said a topic for future documentation improvements. In addition, I would like to let you know that the documentation is publicly available for contribution on GitHub, just click on the link 'Improve this article' (usually placed on the right side of the currently selected article page) and open an pull request. We look forward to seeing more people take this opportunity.

Regards,


Ivo
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Josh
Top achievements
Rank 1
Answers by
Mihail
Telerik team
Josh
Top achievements
Rank 1
Indrayan
Top achievements
Rank 1
Ivo
Telerik team
Share this question
or