Throw

ShouldThrowAction

var homer = new Person { Name = "Homer", Salary = 30000 };
var denominator = 1;
Should.Throw<DivideByZeroException>(() =>
                {
                    var y = homer.Salary / denominator;
                });

snippet source | anchor

Exception

`var y = homer.Salary / denominator;`
    should throw
System.DivideByZeroException
    but did not

ShouldThrowAsync

Task doSomething() => Task.CompletedTask;
var exception = await Should.ThrowAsync<DivideByZeroException>(() => doSomething());

snippet source | anchor

Exception

Task doSomething() should throw System.DivideByZeroException but did not

ShouldThrow Action Extension

var homer = new Person { Name = "Homer", Salary = 30000 };
var denominator = 1;
var action = () =>
                {
                    var y = homer.Salary / denominator;
                };
action.ShouldThrow<DivideByZeroException>();

snippet source | anchor

Exception

`action()`
    should throw
System.DivideByZeroException
    but did not

ShouldThrowFunc

Should.Throw<ArgumentNullException>(() => new Person("Homer"));

snippet source | anchor

Exception

`new Person("Homer")`
    should throw
System.ArgumentNullException
    but did not

ShouldThrow Func Extension

var func = () => new Person("Homer");
func.ShouldThrow<ArgumentNullException>();

snippet source | anchor

Exception

`func()`
    should throw
System.ArgumentNullException
    but did not

ShouldThrowFuncOfTask

var homer = new Person { Name = "Homer", Salary = 30000 };
var denominator = 1;
Should.Throw<DivideByZeroException>(() =>
                {
                    var task = Task.Factory.StartNew(
                        () =>
                        {
                            var y = homer.Salary / denominator;
                        });
                    return task;
                });

snippet source | anchor

Exception

Task `var task = Task.Factory.StartNew( () => { var y = homer.Salary / denominator; }); return task;`
    should throw
System.DivideByZeroException
    but did not

Last updated