-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Assert.IsType<T>(object obj) #413
Comments
@spottedmahn : Would this help? It does have the specific example you referred to. I agree that this would be nice addition in-box though. |
Hi @AbhitejJohn - that is close! It should return the strongly typed object if it is that type IMO. public static class SampleAssertExtensions
{
public static T IsOfType<T>(this Assert assert, object obj)
{
if(obj is T result)
{
return result;
}
throw new AssertFailedException("Type does not match");
}
} |
I like this btw |
@spottedmahn : yeah, I like the chaining a lot too. Makes things easier to express and its readable. Agree on the strong name typing. You might want to add that into a custom extension in your code base or possibly also add that out as a library. I've created one for asserts over exception handling based off of this but I can imagine others extending this out for type checking/comparison too. |
Hey @AbhitejJohn - that's brilliant 💲... create an extension library that can move much faster than the full library 🌉. Like VS Power Tools! I'll put in PR for |
I actually just put that out, so I can go crazy with this extension point and also see if it works for consumers. From the download count, it does look useful. The core framework would still want to update itself with a few of these APIs that everyone would need just so there isn't an extra step to find and pull in another package but that's a choice for the framework now. |
I think it is great, put something out there. If it works and gains traction, put it in the core lib! |
+1 on this, this would clean up so many of my tests. |
Description
xUnit has a nice Assert method:
public static T IsType<T>(object @object)
that is lacking from MS Test.MS Test has an "old" method:
Assert.IsInstanceOfType(object, typeof(object))
. This feels very .Net 1.1 w/o generics.xUnit Advantages
Code Comparison
vs
Open to Adding?
What to do you think about extending the Assert API?
Reference: SO Post: xUnit.net IsType Equivalent in MS Test That Returns Type
AB#1614478
The text was updated successfully, but these errors were encountered: