NullReferenceException with Assert.That and NunitLite - nunit

I'm using NunitLite for my unit testing for a Xamarin project.
Environment:
Windows 8.1 64 bit
Visual Studio 2012
Test project class library referencing .NET 4.5
NunitLite V1.0.0 package from NuGet - contains a build for .NET 4.5
Running tests using Nunit Test Adapter V1.2 from NuGet
With even the simplest test:
[Test]
public void SimpleTest()
{
Assert.That(1 == 1);
}
I'm getting the exception:
System.NullReferenceException was unhandled by user code
HResult=-2147467261 Message=Object reference not set to an instance
of an object. Source=nunitlite StackTrace:
at NUnit.Framework.Assert.IncrementAssertCount()
at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.That(Boolean condition)
at SportLogger.Tests.Unit.Core.when_using_venue_manager.SimpleTest() in
c:\SportLogger\SportLogger.Tests.Unit.Core\CoreTests.cs:line 65 InnerException:
Does anyone have any pointers as to what the problem is?

I solved this by using standard Nunit, not NunitLite. I'm not sure why NunitLite doesn't work in a standard Windows test library.
Bearing in mind that NunitLite is meant to be used (in this context) on the iOS and Android projects, switching to Nunit for the core test project, and NunitLite for the iOS and Android test projects fixed the problem.

Related

Using VS2019, .Net 5.0, Winforms.Wasm 1.0.19137.60 Nuget package - produces unhandled Mono.Cecil.AssemblyResolutionExceptions

I am trying to port 6 class libraries developed under .Net 4.5.2 to .Net 5.00 using VS 2019. I chose .Net 5.00 (and VS 2019) as my final goal is to deliver the solution as a web assembly. I have set the target framework for each of the libaries to .NET 5.0.
One of my class libraries uses Winforms so I have downloaded the Nuget package Winforms.Wasm (1.0.191137.60) by Roozbeh Gh into VS 2019.
On building I initially got the error:
1>C:\Users\howar_000\.nuget\packages\winforms.wasm\1.0.19137.60\build\netstandard2.0\Winforms.Wasm.targets(13,5): error :
Unhandled Exception: Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Windows.Forms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
... at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Run(String[] args)
... at Driver.Main(String[] args)
I was able to get past this problem by adding:
<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
as per instructions I found on the Winforms.Wasm GIT repository recommended by Roozbeh Gh.
1>C:\Users\howar_000\.nuget\packages\winforms.wasm\1.0.19137.60\build\netstandard2.0\Winforms.Wasm.targets(13,5): error :
Unhandled Exception: Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Formats.Asn1, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 1>C:\Users\howar_000\.nuget\packages\winforms.wasm\1.0.19137.60\build\netstandard2.0\Winforms.Wasm.targets(13,5): error : at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Import(String ra, AssemblyKind kind)
... at Driver.Run(String[] args)
i.e. it just moved the problem along to another library distributed along with Winforms.Wasm. I have also tried to explicitly exclude the Nuget version of the System.Formats.Asn.dll but this hasnt worked either.
I realise the problem is that the Nuget Winforms.Wasm package includes netstardard2.0 libraries so that the package download is as a complete standalone distribution. Further that VS 2019 MSBuild is identifying a clash with the dotnet net5.0 distribution.
I have done a lot of investigation trying to resolve this problem - even overwriting
C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0\ref\net5.0\System.Formats.Asn1.dll
with
C:\Users\howar_000.nuget\packages\system.formats.asn1\5.0.0\lib\netstandard2.0\System.Formats.Asn1.dll
and visa versa but to no avail.
But reading through swathes of literature on the web, I came to the realisation that this is a recurring problem that has plagued developers trying to use VS with .Net and Nuget packages for some time. Surely either Microsoft need to change the implementation of VS MSBuild to accommodate Nuget packages or Nuget package writers need to come up with a VS compliant distribution - rather than leaving developers spending many hours trying to establish work arounds and understand the inner works of VS MSBuild.
I am thinking of moving away from VS 2019 to use the Mono Developer toolkit - I will let you know how I get on.
Any help/suggestions on how best to migrate .Net 4.5.2 assemblies that use Nuget packages to equivalent web assemblies would be much appreciated.

Can't create SqlParameter in EF Core 3.1.2 in Xamarin.Android application

I have Android Xamarin.Forms project. It depends on Db project with EF Core code (SQL Server) and targeted .Net Standard 2.0. Both projects in one solution.
With EF Core 2.2.6 everything works fine. When I switch to EF Core 3.1.2, the project does not work anymore.
I made the following changes to compile it:
I added Microsoft.Data.SqlClient 1.1.1 nuget project reference and use namespace Microsoft.Data.SqlClient instead of System.Data.SqlClient
Replace DbSet<Entitiy>.FromSql calls with DbSet<Entitiy>.FromSqlRaw
When I create SqlParameter and try to access it I get an error.
E.g. when I run the following code:
try
{
var sqlParam = new SqlParameter("aaa", "bbb");
var value = sqlParam.Value; // exception thrown here
}
catch (Exception ex)
{
Debug.WriteLine($"Exception: {ex.GetType()}");
Debug.WriteLine($"Message: {ex.Message}");
Debug.WriteLine($"StackTrace: {ex.StackTrace}");
}
I get the following error messages:
Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object
StackTrace: at Microsoft.Data.SqlClient.SqlParameter.get_Value () [0x00000] in E:\SqlClientInternal\agent-1_work\5\s\src\Microsoft.Data.SqlClient
etcore\ref\Microsoft.Data.SqlClient.cs:1222
at [0x00012] in .cs:100
When the sqlParam just assigned, and I want to view its value, I get the following view in Visual Studio:
I use Visual Studio 2019, I tried 16.3.4 and the latest as for now 16.4.6.
How to fix this issue? What is wrong with my code?

TypeInitializationException accessing MonoGame GraphicsAdapter.Adapters, could not load type 'SharpDX.Rectangle'

Converting VB code from XNA 3.1 to MonoGame.
Any attempt to access class GraphicsAdapter throws the exception below. For example, trying to enumerate GraphicsAdapters. On this line:
For Each adapter As GraphicsAdapter In GraphicsAdapter.Adapters
Result is an exception:
System.TypeInitializationException occurred
HResult=-2146233036
Message=The type initializer for 'Microsoft.Xna.Framework.Graphics.GraphicsAdapter' threw an exception.
Source=MonoGame.Framework
TypeName=Microsoft.Xna.Framework.Graphics.GraphicsAdapter
StackTrace:
at Microsoft.Xna.Framework.Graphics.GraphicsAdapter.get_Adapters()
at ...my code...
InnerException:
HResult=-2146233054
Message=Could not load type 'SharpDX.Rectangle' from assembly 'SharpDX, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1'.
Source=MonoGame.Framework
TypeName=SharpDX.Rectangle
StackTrace:
at Microsoft.Xna.Framework.Graphics.GraphicsAdapter.CreateAdapter(Adapter1 device, Output monitor)
at Microsoft.Xna.Framework.Graphics.GraphicsAdapter.PlatformInitializeAdapters(ReadOnlyCollection`1& adapters)
at Microsoft.Xna.Framework.Graphics.GraphicsAdapter..cctor()
Note the inner exception:
Could not load type 'SharpDX.Rectangle' from assembly 'SharpDX, ...Version=4.0.1.0, ...
The project references these assemblies (among others):
MonoGame.Framework version=3.6.0.1625
from NuGet "MonoGame.Framework.WindowsDX", file
packages\MonoGame.Framework.WindowsDX.3.6.0.1625\lib\net40\MonoGame.Framework.dll
SharpDX version=4.0.1.0
SharpDX.Direct2D1 version=4.0.1.0
SharpDX.Direct3D9 version=4.0.1.0
SharpDX.DXGI version=4.0.1.0
SharpDX.Mathematics version=4.0.1.0
So I don't see why it would fail this way.
What could be wrong? Not compatible with this SharpDX version?
NOTE: These are all up-to-date packages from NuGet, in Visual Studio 2015. Targeting .Net 4.5.2.
UPDATE
To prove it isn't a problem with SharpDX, I put this line before that GraphicsAdapter line:
Dim testRect As SharpDX.Rectangle = New Rectangle()
This successfully created testRect. No Exception until attempt to access GraphicsAdapter. Also tried all the other Shared (static) properties of GraphicsAdapter, such as GraphicsAdapter.DefaultDevice. Any one fails with same exception.
I posted the same question to MonoGame forums:
http://community.monogame.net/t/solved-typeinitializationexception-accessing-graphicsadapter-adapters-could-not-load-type-sharpdx-rectangle/9803
Cause of problem:
Current release of MonoGame.Framework for WindowsDX (Windows OS + DirectX drivers), 3.6.0.1625 is built against SharpDX 2.6.3, and is not compatible with more recent SharpDX versions.
(Interim) Solution:
The developer branch of MonoGame does target the current SharpDX. Builds can be obtained from MonoGame's teamcity (free registration required).
Tested MonoGame.Framework build 3.7.0.994; it worked with SharpDX 4.0.1:
http://teamcity.monogame.net/viewLog.html?buildId=56483&tab=artifacts&buildTypeId=MonoGame_PackagingWindows
That build installs most SharpDX dlls to
C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Windows
However, SharpDX.Mathematics.dll is currently missing,
so I still install SharpDX from NuGet, rather than using those.
Standard location for MonoGame releases:
(look for at least 3.7, which is not there yet, as I write this):
http://www.monogame.net/downloads/
(If instead of NuGet packages for MonoGame and SharpDX, I had downloaded MonoGame 3.6 installer and used its version of SharpDX dlls at C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Windows, then I would not have had this incompatibility. However, that was not recent enough SharpDX for my purposes. This should be resolved by release of MonoGame 3.7)

Nunitlite-runner.exe - Dll file has no TestFixtures

I'm trying run tests from nunitlite-runner.exe but i got Error message
1) Invalid : /myfile.dll
Has no TestFixtures
I have in source code
[TestFixture]
public class MyClassName
{
[Order(1)]
[Test]
public void Case1()
{
//
}
[Order(2)]
[Test]
public void Case2()
{
//
}
}
How can i fix code or run my tests?
I got this problem with nunit3-console.exe. I had, in my enthusiasm to upgrade to the latest and greatest, updated my NUnit project references to the latest version (3.9 at this writing), along with my console exe (3.7). But it seems that the console exe is a build or two behind the core libraries, so you can't reference a later version of NUnit than the version you have of the console exe.
I downgraded my NUnit project reference to 3.7, and that fixed it for me.

Facebook plugin for unity3D WP8 and METRO

I try to use facebooksdk.net in my WP8 Unity3D Plugin. Facebook.dll and Facebook.Client.dll and their fakes in correct folders, but error occure. Is there any way to fix it?
Error building Player: Exception: Failed to run assembly preprocessor with command line "Temp/StagingArea/Data/Managed\Facebook.Client.dll" -injectCtor -assemblyPath "Temp/StagingArea" -pdb.[Temp/StagingArea/Data/Managed\Facebook.Client.dll]
No symbols for Temp/StagingArea/Data/Managed\Facebook.Client.dll
Injecting ctor
Error while in assembly preprocessor Temp/StagingArea/Data/Managed\Facebook.Client.dll
Object reference not set to an instance of an object.
at AssemblyPreprocessor.Utils.IsBaseType(TypeDefinition definition, String name)
at AssemblyPreprocessor.Utils.IsFromNamespace(TypeDefinition definition, String namezpace)
at AssemblyPreprocessor.ConstructorInjector.ShouldInject(TypeDefinition typeDefinition)
at AssemblyPreprocessor.ConstructorInjector.InjectConstructorsForTypesIn(ModuleDefinition moduleDefinition)
at AssemblyPreprocessor.Program.Main(String[] args)
Facebook.dll use .NET Framework 4.5 libraries, and Windows Phone use .NET API for windows phone (http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207211(v=vs.105).aspx). You cant export this dll to WP8