0

In nUnit 3, there's been considerable changes made, among which the ExpectedException has been dropped to be replaced by the assertion model.

I've found this and this example but the recommendation is to use Assert.That() instead so I want to use this approach as shown below.

//Arrange
string data = "abcd";
//Act
ActualValueDelegate<object> test = () => data.MethodCall();
//Assert
Assert.That(test, Throws.TypeOf<ExceptionalException>());

However, I'd like to test the assert for a number of different parameters but I can't seem to get it to work. I've tried the following.

List<List<Stuff>> sets = new[] { 0, 1, 2 }
  .Select(_ => Enumerable.Repeat(new Stuff(_), _).ToList())
  .ToList();
Assert.Throws(() => Transform.ToThings(new List<Stuff>()));

The above works as to creating the list of lists, as desired and testing the Transform.Stuff() call. However, I haven't figured out a way to plug sets into things.

Is it possible? Where are the examples? (Most of the searches drown in nUnit 2.x and on the official documentation site, I see nothing that helps me.)

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

3

You should look into using the TestCaseSource attribute to define the inputs to your method:

[TestCaseSource("StuffSets")]
public void ToThings_Always_ThrowsAnException(List<Stuff> set)
{
    // Arrange
    // Whatever you need to do here...

    // Act
    ActualValueDelegate<object> test = () => Transform.ToThings(set);

    // Assert
    Assert.That(test, Throws.TypeOf<SomeKindOfException>());
}

public static IEnumerable<List<Stuff>> StuffSets = 
{
    get 
    {
        return new[] { 0, 1, 2 }
            .Select(_ => Enumerable.Repeat(new Stuff(_), _).ToList())
            .ToList();
    }
};

This will invoke ToThings_Always_ThrowsAnException once for each List<Stuff> that is returned from StuffSets (here, three times).

Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
  • It surprises me that the input isn't brought in from the parentheses at *test = () => ...* instead. Am I totally off feeling that it's the lambda expression that should be able to produce the input to the transforming method? – Konrad Viltersten Sep 26 '16 at 15:36
  • Maybe, but it is a design decision the NUnit team made; they chose the `TestCaseSource`, `ValueSource`, and other ways of parameterizing tests with input instead of creating an API on `Assert` to do it. – Patrick Quirk Sep 26 '16 at 18:20
  • Answer accepted and +1'ed. I have one minor follow-up, though. When you say that the nUnit team made a design decision, is it something you infer from the circumstances or is there an actual source of that information? Rationale behind it? I want to emphasize that I by no means question your statement. I'm just curious. – Konrad Viltersten Sep 26 '16 at 20:40
  • I only say that because of course one *could* implement it in the way you describe in your question, but that's just not how the NUnit team implemented it. The `TestCaseSource` was introduced so long ago I doubt I could dig enough to find any online record of why they went down the road they did. – Patrick Quirk Sep 27 '16 at 12:29