Quantcast
Channel: Fluent Assertions
Viewing all articles
Browse latest Browse all 1402

New Post: Best way to test for exception on class's Default Indexer Property

$
0
0

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


Viewing all articles
Browse latest Browse all 1402

Trending Articles