Given the following class:
class MyClass { private List<string> myList; string this[int index] { return myList[index]; } }
I can use it like this:
string TestValue = myClass[5];
But how do I test if this throws an error? I came up with this--obfuscating the test by adding in the superfluous "ToString()" call, but I was hoping for something more readable:
myClass.Invoking(x => x[-1].ToString())
.ShouldThrow<IndexOutOfRangeException>("because -1 is obviously an invalid index");
It would be awesome if it could be something like:
myClass[-1].ShouldThrow<IndexOutOfRangeException>("because...");
Is there some other way to test this that I'm overlooking?
Thanks!
Tony