shouldly / shouldly

Should testing for .NET—the way assertions should be!

Home Page:https://docs.shouldly.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DynamicShould.HaveProperty assumes cast to IDictionary<string, object>

marco-luzzara opened this issue · comments

In DynamicShould.HaveProperty, if the object we pass is of type IDynamicMetaObjectProvider, but does not implement IDictionary<string, object>, an InvalidCastException is thrown.

This does not happen with expando objects, but Shouldly cannot assert the property of a class that directly implements System.Dynamic.DynamicObject, for example.

if (dynamicTestObject is IDynamicMetaObjectProvider)
{
var dynamicAsDictionary = (IDictionary<string, object>)dynamicTestObject;

Thanks for the report! My guess is we should be using the IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression) method instead. Possibly for correctness even in the case that the type does implement IDictionary<string, object>.

Pull requests welcome.

I would like to work on it but I cannot compile the solution. I am using docker (mcr.microsoft.com/dotnet/sdk:6.0) because I don't have the last version installed but when I run:

dotnet build src/Shouldly.Tests/Shouldly.Tests.csproj

I get

/app/src/Shouldly.Tests/Shouldly.Tests.csproj : error NU1505: Duplicate 'PackageDownload' items found. Remove the duplicate items or use the Update functionality to ensure a consistent restore behavior. The duplicate 'PackageDownload' items are: Microsoft.NETCore.App.Ref [3.1.0], Microsoft.NETCore.App.Ref [3.1.0].

The same goes for dotnet build on the solution. It only works when I build the Shouldly.csproj.