1

I'm using Microsoft's Visual Studio unit testing framework (the project does therefore I have to). I'm sorely missing some of the more advanced assertions such as AreElementsEqual you find in MBUnit.

I'd like to make them.

As the class is static I can't inherit from it (to create a SuperAssert) and I can't add an extension method (as they're static methods).

I don't want to simply create another class and expect consumers to use the two different ones. How can I expand the class?

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
Liath
  • 9,913
  • 9
  • 51
  • 81
  • `I can't add an Extension Method (as they're static methods).` why? could you give some code example on why you can't add Extension methods? – Ilya Ivanov Mar 25 '13 at 11:04
  • 2
    For AreElementsEqual, have you checked that [CollectionAssert](http://msdn.microsoft.com/en-us/library/ms245294.aspx) doesn't meet your needs? – Jon Skeet Mar 25 '13 at 11:05
  • 2
    @IlyaIvanov: You simply can't add an extension method that can be called on class name as opposed to an instance. I don't see what you need a code sample for - or what it should show. – Daniel Hilgarth Mar 25 '13 at 11:06
  • @DanielHilgarth you mean non-static method? Extension methods are static methods, which can be used to extend\add more behavior, which I guess OP wants. – Ilya Ivanov Mar 25 '13 at 11:07
  • @IlyaIvanov: Extension methods are static methods. That's correct. But they can only be called on instances of an object, not on a type name. Example: You can't create an extension method that allows this code: `int.MyExtension()`. You can only create one that can be called like this: `int i = 0; i.MyExtension();`. – Daniel Hilgarth Mar 25 '13 at 11:08
  • @DanielHilgarth yes, sure, I understand extension method syntax and purpose. OP wrote `I don't want to simply create another class and expect consumers to use the two different ones. How can I expand the class?` this can be achieved by extension methods. FluentAssertion, by the way, use the same idea (extensions to object) and the main constraint of OP was `expect consumers to use the two different ones`, which is also solved by extension methods – Ilya Ivanov Mar 25 '13 at 11:13
  • @IlyaIvanov: You don't seem to understand that the `Assert` class the OP wants to extend is a static class with static methods. You don't create an instance of it, hence you **can't** extend it using extension methods. – Daniel Hilgarth Mar 25 '13 at 11:14
  • @JonSkeet I didn't know about them - handy! I think in this case I'll struggle because I'm unit testing generic IEnumerables but I'll remember them for next time! – Liath Mar 25 '13 at 11:16
  • @DanielHilgarth ok ok, let's close this discussion. I was wrong assuming that OP can use extension methods on objects, as fluent syntax for state verification – Ilya Ivanov Mar 25 '13 at 11:18

2 Answers2

3

You can't. You will have to create a new one.

Or you could create an existing package, like Fluent Assertions.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

As the class is static, you cannot as you say, use extension methods to 'add' further methods to the class.

The closest you can do within reason is the following:

public static class AssertExtensions
{
    public static void SuperAssert(bool expression)
    {
        // etc...
    }
}

If you are producing a tool library, asking the user to use another class should not be a problem.

If you are still concerned, why not create a base class for your test and have the users use methods within that for asserts?

For instance:

public class TestBase
{
    protected void AreEqual(object obj1, object obj2)
    {
        Assert.AreEqual(obj1, obj2); // etc...
    }

    protected void SuperAssert(bool expression)
    {
        // etc...
    }
}
rhughes
  • 9,257
  • 11
  • 59
  • 87