Skip to content
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

Closed
spottedmahn opened this issue Apr 30, 2018 · 8 comments · Fixed by #1241
Closed

Add Assert.IsType<T>(object obj) #413

spottedmahn opened this issue Apr 30, 2018 · 8 comments · Fixed by #1241
Assignees
Labels
Milestone

Comments

@spottedmahn
Copy link
Contributor

spottedmahn commented Apr 30, 2018

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

@AbhitejJohn
Copy link
Contributor

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

@spottedmahn
Copy link
Contributor Author

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");
	}
}

@spottedmahn
Copy link
Contributor Author

I like this btw Assert.That.IsNotNull(animal).And.IsOfType<Cat>(animal)! 👍⚡

@AbhitejJohn
Copy link
Contributor

@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.

@spottedmahn
Copy link
Contributor Author

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 public static T IsOfType<T>(this Assert assert, object obj)!

@AbhitejJohn
Copy link
Contributor

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
Copy link
Contributor Author

I think it is great, put something out there. If it works and gains traction, put it in the core lib!

@pascalpfeil
Copy link

+1 on this, this would clean up so many of my tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants