Shouldly
Search…
NotThrow

ShouldNotThrowAction

1
var homer = new Person { Name = "Homer", Salary = 30000 };
2
var denominator = 0;
3
Should.NotThrow(() =>
4
{
5
var y = homer.Salary / denominator;
6
});
Copied!
Exception
1
`var y = homer.Salary / denominator;`
2
should not throw but threw
3
System.DivideByZeroException
4
with message
5
"Attempted to divide by zero."
Copied!

ShouldNotThrow Action Extension

1
var homer = new Person { Name = "Homer", Salary = 30000 };
2
var denominator = 0;
3
Action action = () =>
4
{
5
var y = homer.Salary / denominator;
6
};
7
action.ShouldNotThrow();
Copied!
Exception
1
`action()`
2
should not throw but threw
3
System.DivideByZeroException
4
with message
5
"Attempted to divide by zero."
Copied!

ShouldNotThrowFunc

1
string? name = null;
2
Should.NotThrow(() => new Person(name!));
Copied!
Exception
1
`new Person(name!)`
2
should not throw but threw
3
System.ArgumentNullException
4
with message
5
"Value cannot be null. (Parameter 'name')"
Copied!

ShouldNotThrow Func Extension

1
string? name = null;
2
Func<Person> func = () => new Person(name!);
3
func.ShouldNotThrow();
Copied!
Exception
1
`func()`
2
should not throw but threw
3
System.ArgumentNullException
4
with message
5
"Value cannot be null. (Parameter 'name')"
Copied!

ShouldNotThrowFuncOfTask

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