Shouldly
Search…
Throw

ShouldThrowAction

1
var homer = new Person { Name = "Homer", Salary = 30000 };
2
var denominator = 1;
3
Should.Throw<DivideByZeroException>(() =>
4
{
5
var y = homer.Salary / denominator;
6
});
Copied!
Exception
1
`var y = homer.Salary / denominator;`
2
should throw
3
System.DivideByZeroException
4
but did not
Copied!

ShouldThrowAsync

1
Func<Task> doSomething = async () =>
2
{
3
await Task.Delay(1);
4
};
5
var exception = await Should.ThrowAsync<DivideByZeroException>(() => doSomething());
Copied!
Exception
Task doSomething() should throw System.DivideByZeroException but did not

ShouldThrow Action Extension

1
var homer = new Person { Name = "Homer", Salary = 30000 };
2
var denominator = 1;
3
Action action = () =>
4
{
5
var y = homer.Salary / denominator;
6
};
7
action.ShouldThrow<DivideByZeroException>();
Copied!
Exception
1
`action()`
2
should throw
3
System.DivideByZeroException
4
but did not
Copied!

ShouldThrowFunc

1
Should.Throw<ArgumentNullException>(() => new Person("Homer"));
Copied!
Exception
1
`new Person("Homer")`
2
should throw
3
System.ArgumentNullException
4
but did not
Copied!

ShouldThrow Func Extension

1
Func<Person> func = () => new Person("Homer");
2
func.ShouldThrow<ArgumentNullException>();
Copied!
Exception
1
`func()`
2
should throw
3
System.ArgumentNullException
4
but did not
Copied!

ShouldThrowFuncOfTask

1
var homer = new Person { Name = "Homer", Salary = 30000 };
2
var denominator = 1;
3
Should.Throw<DivideByZeroException>(() =>
4
{
5
var task = Task.Factory.StartNew(
6
() =>
7
{
8
var y = homer.Salary / denominator;
9
});
10
return task;
11
});
Copied!
Exception
1
Task `var task = Task.Factory.StartNew( () => { var y = homer.Salary / denominator; }); return task;`
2
should throw
3
System.DivideByZeroException
4
but did not
Copied!
Last modified 9mo ago