How to find the NotSupportedException root cause? - c#

I have a netcoreapp2.0 test project running against a .net 4.6.1 project. I've started having NotSupportedException thrown on all tests in this project after installing the 3.0 SDK. On machines with only older SDKs or specifying the 2.2 SDK on the global.json file it works.
My point is, how can I find what is throwing the exception? It's being thrown inside a DLL which I don't own and I can't use sourcelink. VS is not displaying any stacktrace and it doesn't stop on the exception even with CLR exceptions set for every exception.
This is the only result I get, by running the tests.
PS (...)> dotnet test
Test run for (...).Tests.dll(.NETCoreApp,Version=v2.2)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:01.35] (...) [FAIL]
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
(REPEATS FOR ALL THE TESTS...)
Test Run Failed.
Total tests: 30
Failed: 30
Total time: 3,2953 Seconds
Update:
I don't know how or why after updating the VS to 16.3.9 (I was on 16.3.8) and/or restarting the machine it didn't happen anymore. All the tests are now working without any modification to the source.
git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Test run for (...).Tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Test Run Successful.
Total tests: 431
Passed: 431
Total time: 10,5425 Seconds
Although I couldn't reproduce the error in the exact same scenario, I could get the same error message on purpose and perhaps it's related.
All our tests are decorated with Xunit.TheoryAttribute, a couple of Xunit.TraitAttribute and many JsonDataSourceAttribute. This last one is developed inhouse and creates a test scenario from a JSON file, pretty much like DataSourceAttribute does (like explained on How to: Create a data-driven unit test).
Even activating "Break When Thrown" and adding breakpoints on the test method and constructor, when the test scenario file can't be found I get this same error message and no stacktrace is provided.
TestClassName
Source: (...)Test.cs line 35
Duration: 1 ms
Message:
System.NotSupportedException : Specified method is not supported.
Perhaps the previous problem was related to the files not being copied to the output, but I still don't know why I don't get a stacktrace or a more specific error related to the file not being found. I'll check the attribute source and verify if it could be improved and if it's, somehow, hiding the issue.

By adding the JsonDataSourceAttribute source to the same solution I could make it stop where it should (on the "file not found", in this case).
This is the original source. The exception (when forced to happen) is thrown on if (!File.Exists(inputfile)), but Xunit.Sdk.DataAttribute hides it.
public sealed class JsonDataSourceAttribute : DataAttribute
{
public string InputFilePath { get; }
/// <summary>
/// Retrieves a collection of context data
/// </summary>
/// <param name="inputFilePath">Json source file path</param>
public JsonDataSourceAttribute(string inputFilePath)
{
this.InputFilePath = inputFilePath;
}
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
if (testMethod == null) { throw new ArgumentNullException(nameof(testMethod)); }
var inputfile = Path.IsPathRooted(this.InputFilePath)
? this.InputFilePath
: Path.Combine(Directory.GetCurrentDirectory(), this.InputFilePath);
if (!File.Exists(inputfile))
{
throw new ArgumentException($"Input file not found at '{inputfile}'.");
}
return this.LoadFileData(inputfile, this.EnumMap);
}
(...)
}
By moving the file existence check to the constructor, I was able to get what I wanted.
public JsonDataSourceAttribute(string inputFilePath)
{
var inputfile = Path.IsPathRooted(inputFilePath)
? inputFilePath
: Path.Combine(Directory.GetCurrentDirectory(), inputFilePath);
if (!File.Exists(inputfile))
{
throw new ArgumentException($"Input file not found at '{inputfile}'.");
}
this.InputFilePath = inputfile;
}
This is not the "find the root cause" answer, but I guess it wouldn't be possible since the original exception is omitted on Xunit.Sdk.DataAttribute.

Related

What does the xunit console output (for a failing test) actually mean?

On the linux command line I created a simple xunit test project with :
$ dotnet new xunit
In the file UnitTest1.cs , created as part of the xunit project template, I entered the following simplest possible failing test with a single line Assert.Equal(1,2); ... like so :
namespace trash;
public class UnitTest1
{
[Fact]
public void Test1()
{
Assert.Equal(1,2);
}
}
The output of the failing test is :
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.43] trash.UnitTest1.Test1 [FAIL]
Failed trash.UnitTest1.Test1 [4 ms]
Error Message:
Assert.Equal() Failure
Expected: 1
Actual: 2
Stack Trace:
at trash.UnitTest1.Test1() in
[pathtoMyProjects/Projects/trash/UnitTest1.cs:line 9
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Failed! - Failed: 1, Passed: 0, Skipped: 0, Total: 1, Duration: < 1 ms - trash.dll (net7.0)
I have two simple questions:
What does [xUnit.net 00:00:00.43] mean? -- specifically what does this number 43 represent ?
Why do we see this Stack Trace ? -- specifically , where was this exception thrown, who caught it , and what are we supposed to do with this ?
I searched stackoverflow, xunit documentation, and google -- but it was surprisingly hard to get answers to these simple questions. Can anyone please answer these questions -- or point me to a resource where this output is explained.

How do I get the full file name for failed .NET tests?

I've never used dotnet/.NET for unit tests before so I'm very new to how it all works. I'm starting work on an existing project with many unit tests already createdin C# and Lua, and after running those existing tests, it shows that 454 have passed and 2 have failed, but I can't seem to find out which tests specifically failed.
When I run dotnet test through Windows Powershell, I get the following output:
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:19.7411114] tests.ScriptedTests.RunTest(testFile: "C:\\Users\\ccort\\OneDrive\\Documents\\Epic stuff "...) [FAIL]
X tests.ScriptedTests.RunTest(testFile: "C:\\Users\\ccort\\OneDrive\\Documents\\Epic stuff "...) [55ms]
Error Message:
Assert.Equal() Failure
Expected: 0
Actual: 4
Stack Trace:
at tests.ScriptedTests.AssertEqual(Int32 v1, Int32 v2) in C:\Users\ccort\OneDrive\Documents\Epic stuff for my job\epictourneyserver\tests\ScriptedTests.cs:line 26
at MoonSharp.Interpreter.Interop.MethodMemberDescriptor.Execute(Script script, Object obj, ScriptExecutionContext context, CallbackArguments args)
at MoonSharp.Interpreter.Interop.FunctionMemberDescriptorBase.<>c__DisplayClass29_0.<GetCallback>b__0(ScriptExecutionContext c, CallbackArguments a)
at MoonSharp.Interpreter.CallbackFunction.Invoke(ScriptExecutionContext executionContext, IList`1 args, Boolean isMethodCall)
at MoonSharp.Interpreter.Execution.VM.Processor.Internal_ExecCall(Int32 argsCount, Int32 instructionPtr, CallbackFunction handler, CallbackFunction continuation, Boolean thisCall, String debugText, DynValue unwindHandler)
at MoonSharp.Interpreter.Execution.VM.Processor.Processing_Loop(Int32 instructionPtr)
at MoonSharp.Interpreter.Execution.VM.Processor.Call(DynValue function, DynValue[] args)
at MoonSharp.Interpreter.Script.Call(DynValue function)
at MoonSharp.Interpreter.Script.DoString(String code, Table globalContext, String codeFriendlyName)
at tests.ScriptedTests.RunTest(String testFile) in C:\Users\ccort\OneDrive\Documents\Epic stuff for my job\epictourneyserver\tests\ScriptedTests.cs:line 138
[xUnit.net 00:00:23.2775868] tests.ScriptedTests.RunTest(testFile: "C:\\Users\\ccort\\OneDrive\\Documents\\Epic stuff "...) [FAIL]
X tests.ScriptedTests.RunTest(testFile: "C:\\Users\\ccort\\OneDrive\\Documents\\Epic stuff "...) [83ms]
Error Message:
Assert.Equal() Failure
Expected: 1
Actual: 0
Stack Trace:
at tests.ScriptedTests.AssertEqual(Int32 v1, Int32 v2) in C:\Users\ccort\OneDrive\Documents\Epic stuff for my job\epictourneyserver\tests\ScriptedTests.cs:line 26
at MoonSharp.Interpreter.Interop.MethodMemberDescriptor.Execute(Script script, Object obj, ScriptExecutionContext context, CallbackArguments args)
at MoonSharp.Interpreter.Interop.FunctionMemberDescriptorBase.<>c__DisplayClass29_0.<GetCallback>b__0(ScriptExecutionContext c, CallbackArguments a)
at MoonSharp.Interpreter.Execution.VM.Processor.Internal_ExecCall(Int32 argsCount, Int32 instructionPtr, CallbackFunction handler, CallbackFunction continuation, Boolean thisCall, String debugText, DynValue unwindHandler)
at MoonSharp.Interpreter.Execution.VM.Processor.Processing_Loop(Int32 instructionPtr)
at MoonSharp.Interpreter.Execution.VM.Processor.Call(DynValue function, DynValue[] args)
at MoonSharp.Interpreter.Script.Call(DynValue function)
at MoonSharp.Interpreter.Script.DoString(String code, Table globalContext, String codeFriendlyName)
at tests.ScriptedTests.RunTest(String testFile) in C:\Users\ccort\OneDrive\Documents\Epic stuff for my job\epictourneyserver\tests\ScriptedTests.cs:line 138
Test Run Failed.
Total tests: 456
Passed: 454
Failed: 2
Total time: 27.1261 Seconds
---------------------------------
Notice how it lists the failed file name as "C:\Users\ccort\OneDrive\Documents\Epic stuff "..., which is quite useless when it comes to looking for the file in my computer. Using Visual Studio's Test Explorer gives the same exact issue, only showing the beginning of the failed test's file path rather than the entire file path or even just the file's name.
How can I find the exact file name or full file path of tests that fail? I'm looking for the full string parameter "testFile", which gets passed to the method ScriptedTests.RunTest().

Vs2019.9.2 Raise runtime errors when using Property Pattern for undefined variable and fail compilation and it's expected compilation error

Using Vs 2019.9.2 (or v 2019.9.0):
Create a console net5 application.
Add the next class:
class Class1
{
public void Test1()
{
if (shape is Circle { Radius: >= 100 }) //variable shape is un defined, cause many errors by vs 2019.9.2
{
// this is a huge circle
}
}
}
public class Circle
{
public double Radius { get; init; }
}
You get many error messages (without compilation) at the upper screen under the menu with these common errors:
Feature 'Diagnostic analyzer runner' is currently unavailable due to an internal error. Show Stack Trace
Feature 'CodeLens references' is currently unavailable due to an internal error. Show Stack Trace
Feature 'Semantic classification cache' is currently unavailable due to an internal error. Show Stack Trace
The common error message in Stack Trace:
RPC server exception:
System.InvalidOperationException: Operation is not valid due to the current state of the object.
as shown in the next figure:
and you can't remove these errors, then vs2019 raise the compilation error:
Error MSB6006 "csc.exe" exited with code -2146232797. Vs2019Bug C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets 71
The line ( with undefined variable shape)
if (shape is Circle { Radius: >= 100 })
cause these errors, and it's expected that vs 2019 raise a compilation error.
Remove the Property Patten check and replace the above line with the next one:
if (shape is Circle ) //cause compilation error as expected
What I missed to avoid the VS2019 runtime error?
I have posted the issue to the Roslyn Project
here
It was reported as a bug and the issue is resolved by Roslyn Team in the next version of Vs 2019.
Thanks to Roslyn team.

Unit Testing Using Sitecore.FakeDb Throws Sitecore.Nexus instantiation error on GetItem

I'm a sitecore developer and I want to use Sitecore.FakeDb in my unit testing. I have the following code:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Test_ArticleController_With_SitecoreItem()
{
Sitecore.Data.ID about_us_itemId = Sitecore.Data.ID.NewID;
Sitecore.Data.ID careers_itemId = Sitecore.Data.ID.NewID;
using (var db = new Sitecore.FakeDb.Db
{
new Sitecore.FakeDb.DbItem("about-us", about_us_itemId)
{
new Sitecore.FakeDb.DbField(SitecoreFieldIds.WTW_REDIRECT_TO) { Value = "/WTW-Home/about-us/overview" }
},
new Sitecore.FakeDb.DbItem("careers", careers_itemId)
{
new Sitecore.FakeDb.DbField(SitecoreFieldIds.WTW_REDIRECT_TO) { Value = "http://careers.willistowerswatson.com" }
}
})
{
Sitecore.Data.Items.Item sampleItem3 = db.GetItem(about_us_itemId); // throws error
Sitecore.Data.Items.Item sampleItem2 = db.GetItem("/sitecore/content/careers"); //throws error
//Assert
Assert.AreEqual("abc", "abc");
};
}
}
I get the following error when I try either of the two above .GetItem(...) lines:
System.TypeInitializationException: 'The type initializer for 'Sitecore.SecurityModel.License.LicenseManager' threw an exception.'
InvalidOperationException: Could not instantiate the type 'Sitecore.Nexus.Licensing.NexusLicenseApi, Sitecore.Nexus'
I have included the following relevant references (amongst others) in my unit testing project: Sitecore.FakeDb, Sitecore.Kernel, Sitecore.Mvc, Moq, Glass.Mapper, Glass.Mapper.Sc, Glass.Mapper.Sc.Mvc, Castle.Core. Do I need Sitecore.Nexus? If yes, where can I get it from?
I have an App_Config/Include/Sitecore.FakeDb.config file, as well as an App_Config/app.config file, which has this setting in it:
<sitecore>
<settings>
<setting name="LicenseFile" value="..\..\license.xml" />
</settings>
</sitecore>
Any idea on why this error is occuring and how to exactly fix it?
Here is the full stacktrace, if that helps:
System.TypeInitializationException occurred
HResult=0x80131534
Message=The type initializer for 'Sitecore.SecurityModel.License.LicenseManager' threw an exception.
Source=Sitecore.Kernel
StackTrace:
at Sitecore.SecurityModel.License.LicenseManager.DemandRuntime(Boolean acceptExpress)
at Sitecore.Data.Managers.DefaultItemManager.get_FallbackProvider()
at Sitecore.Data.Managers.DefaultItemManager.<>c__DisplayClass2f.<GetItem>b__2e()
at Sitecore.Data.Managers.DefaultItemManager.ExecuteAndReturnResult[TArgs,TResult](String pipelineName, String pipelineDomain, Func`1 pipelineArgsCreator, Func`1 fallbackResult)
at Sitecore.Data.Managers.DefaultItemManager.GetItem(ID itemId, Language language, Version version, Database database, SecurityCheck securityCheck)
at Sitecore.Data.Managers.DefaultItemManager.GetItem(ID itemId, Language language, Version version, Database database)
at Sitecore.Data.Managers.ItemManager.GetItem(ID itemId, Language language, Version version, Database database)
at Sitecore.Data.DefaultDatabase.GetItem(ID itemId)
at Sitecore.FakeDb.Db.GetItem(ID id)
at WTW.Feature.HomeBottomContent.Tests.UnitTest1.Test_ArticleController_With_SitecoreItem() in C:\dev\TowersWatson\DEV\Maintenance\Source\Feature\HomeBottomContent\Tests\UnitTest1.cs:line 76
Inner Exception 1:
InvalidOperationException: Could not instantiate the type 'Sitecore.Nexus.Licensing.NexusLicenseApi, Sitecore.Nexus'
You need to have a reference to Sitecore.nexus.dll
In Visual Studio: Tools->Options
In Options window find 'Nuget Package Manager'->'Package Sources'
Then click green plus and fill Name and Source text fields.
Name one - whatever you want.
Source text field fill with 'https://sitecore.myget.org/F/sc-packages/api/v3/index.json'
After that go to Tools->Nuget Package Manager->Manage Nuget Packages for Solution...
In 'Package source' dropdown in right-top corner choose just add source. In my case it is 'Sitecore'. In search field type 'Sitecore.Nexus.Noreferences':
Choose a project you want to install package and click Install button.
The Sitecore.Nexus assembly is in the Bin folder of your Sitecore website. Add a reference to this assembly from your test project.

Visual Studio 2013 Test Explorer StackTrace for failed unit test shows "no source available" instead of link to failed line

I created a simple unit test in a C# project:
[TestMethod]
public void DoSomething()
{
// Act
// assert
Assert.Fail("blah");
}
When I run this, the Test Explorer does not hyperlink to the line that failed (the Assert) and does not show the line number. Instead if I hover over the line it shows "no source available".
Test Name: DoSomething
Test FullName: BlahBlah.DoSomething
Test Source:c:\Src\BlahBlahTest.cs : line 223
Test Outcome: Failed
Test Duration: 0:00:04.1933182
Result Message: Assert.Fail failed. blah
Result StackTrace: at BlahBlahTest.DoSomething()
I deleted the .suo file and the test is set to full debugging.
I also have Resharper installed and its test runner also doesn't link to the failed line.
Does anybody know how to fix this?

Categories