Type.Should().Be(Type) doesn't support open generics
See original GitHub issueI would expect this:
var response = result.GetType(); // returns ICustomInterface<int>
response.Should().Be(typeof(ICustomInterface<>));
to succeed.
Instead it fails with message:
Expected type to be ICustomInterface1, but found ICustomInterface
1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].
I believe this is related to the change made for: #458
P.S. Workaround for now:
result.GetType().GetGenericTypeDefinition()
.Should().Be(typeof(ICustomInterface<>));
I am also using this in a few “And” constraints which forces me to do:
result.Should().NotBeNull()
.And.Subject.GetGenericTypeDefinition().Should().Be(typeof(ICustomInterface<>));
Which is ugly and stops me being able to add more assertions as the Subject is now the GenericTypeDefinition and not the actual Type I am validating.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
What exactly is an "open generic type" in .NET? [duplicate]
The C# language defines an open type to be a type that's either a type argument or a generic type defined with unknown...
Read more >Open and Closed Generic Types in C# | Pluralsight
In this guide, we will take a look at deeper at what generics are and what their open or closed properties mean.
Read more >Partially closed generic types
An open generic type is simply a generic type whose type parameters have not been specified.
Read more >Documentation - Generics
When you begin to use generics, you'll notice that when you create generic functions like identity , the compiler will enforce that you...
Read more >How To Use Generics in Go
Using generic types allows you to interact directly with your types, leading to cleaner and easier-to-read code. In this tutorial, you will ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for raising this issue. First of all to assert on the type of an object you should probably use one of
to test that
response
is assignable toTType
.The changes related to #458 added support for open generic when using
BeOfType()
.Here are my findings:
First let’s see how Fluent assertions behaves for non-generic types to establish a baseline.
And the results for doing the same assertions on generic types, both with open and closed generic types.
To summarize:
BeOfType(typeof(GenericType<>))
works as expectedBeAssignableTo(typeof(GenericType<>))
does not work as expected.If agree that
BeAssignableTo
should have the same behavior asBeOfType
when asserting on open generic types.Whoever takes up this task, should assure consistency in the behavior between:
BeOfType(Type)
NotBeOfType(Type)
BeAssignableTo(Type)
NotBeAssignableTo(Type)
Currently only
BeOfType(Type)
has support for open generic types.Support for open generic was contributed in #955 and released with Fluent Assertions 5.5.0.