Add Assert.IsType<T>(object obj)
See original GitHub issueDescription
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
- The xUnit method uses generics making it cleaner IMHO
- The xUnit method returns the object (allows for clean one-liner)
Code Comparison
var badRequestResult = Assert.IsType<BadRequestObjectResult>(result);
//do stuff with badRequestResult
var blah = badRequestResult.Value;
...
vs
var badRequestResult = result as BadRequestObjectResult;
Assert.IsInstanceOfType(badRequestResult, typeof(BadRequestObjectResult));
//do stuff with badRequestResult
var blah = badRequestResult.Value;
...
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
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:8 (7 by maintainers)
Top Results From Across the Web
c# - Correct way to unit test the type of an object
AreEqual(Type expected, Type actual) . So, in this case: Assert.AreEqual(typeof(MyObject), obj.GetType());.
Read more >Assert.IsInstanceOfType Method
IsInstanceOfType(Object, Type) Tests whether the specified object is an instance of the expected type and throws an exception if the expected type is...
Read more >Assert That an Object Is From a Specific Type
In this article, we'll explore how we can verify that an object is of a specific type. We'll be looking at different testing...
Read more >xUnit Assertions :: K-State CIS 400 Textbook
For example, xUnit provides two boolean assertions: Assert. ... Assert.IsAssignableFrom<T>(object obj) Where T is the type to cast into.
Read more >Assertions
Package assert provides a set of comprehensive testing tools for use with the normal Go testing system.
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
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.
@spottedmahn : Would this help? It does have the specific example you referred to. I agree that this would be nice addition in-box though.