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

Strictly speaking, you shouldn't have multiple assertions in a single test. The 'single assertion per test case' is a general guideline that many experienced TDD practitioners apply. What I would propose is to use a so-called Parameterized Test.

[TestMethod]
public void When_index_is_valid_it_should_not_throw()
{
  Action action = () => { var testValue = myClass[0]; }
  action.ShouldNotThrow("because...");
}

[TestMethod]
public void When_index_is_negative_it_should_throw()
{
   When_index_is_out_of_range_it_should_throw(-1);
}

[TestMethod]
public void When_index_is_out_of_range_it_should_throw()
{
   When_index_is_out_of_range_it_should_throw(1);
}

private void When_index_is_out_of_range_it_should_throw(int index)
{
  Action action = () => { var testValue = myClass[index]; }
  action.ShouldThrow("because...");
}

Viewing all articles
Browse latest Browse all 1402

Trending Articles