question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Assert.IsType<T>(object obj)

See original GitHub issue

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

  1. The xUnit method uses generics making it cleaner IMHO
  2. 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

AB#1614478

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
AbhitejJohncommented, May 17, 2018

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.

1reaction
AbhitejJohncommented, May 15, 2018

@spottedmahn : Would this help? It does have the specific example you referred to. I agree that this would be nice addition in-box though.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found