Expectable 1.0.0
dotnet add package Expectable --version 1.0.0
NuGet\Install-Package Expectable -Version 1.0.0
<PackageReference Include="Expectable" Version="1.0.0" />
paket add Expectable --version 1.0.0
#r "nuget: Expectable, 1.0.0"
// Install Expectable as a Cake Addin #addin nuget:?package=Expectable&version=1.0.0 // Install Expectable as a Cake Tool #tool nuget:?package=Expectable&version=1.0.0
Expectable
Define an expected exception with optional expectations.
Where do I get it
Expectable can be installed using the Nuget package manager
PM> Install-Package Expectable
or the dotnet CLI.
dotnet add package Expectable
How to get started
Define an ExpectedException.
See Initialization and Expectations below for options.
// using Expectable; ExpectedException expectedException = Expect<ArgumentException>.Where .MessageStartWith("value") .MessageContains("cannot be", StringComparison.OrdinalIgnoreCase) .MessageEndsWith("null");
Catch the exception.
XUnit / NUnit
// string value = null; var exception = Assert.Throws(expectedException.Type, () => DoSomething(value)); // void DoSomething(string value) // { // if (value == null) // throw new ArgumentException("value is null"); // ... // }
Compare the exception to the expected exception.
XUnit
Assert.Equal(expectedException, exception);
Message: Assert.Equal() Failure: Values differ Expected: System.ArgumentException where [+] Message starts with "value" [-] Message contains "cannot be" (ComparisonType: OrdinalIgnoreCase) [+] Message ends with "null" Actual: System.ArgumentException: value was not null at Expectable.Tests.ReadmeExamples.ReadmeExamples.DoSomething(String value) at Xunit.Assert.RecordException(Action testCode)
NUnit
Assert.That<ExpectedException>(exception, Is.EqualTo(expectedException));
Assert.That(exception, Is.EqualTo(expectedException)) Expected: <System.ArgumentException where [+] Message starts with "value" [-] Message contains "cannot be" [+] Message ends with "null"> But was: <System.ArgumentException: value was null at Expectable.Tests.ReadmeExamples.ReadmeExamples.DoSomething(String value) at NUnit.Framework.Assert.Throws(IResolveConstraint expression, TestDelegate code, String message, Object[] args)>
Initialization
There are several ways to initialize an ExpectedException.
See Expectations below for further options.
Implicit Conversion from Expect<TException>
ExpectedException expectedException = Expect<ArgumentException>.Where
.MessageStartsWith("value")
.MessageContains("cannot be", StringComparison.OrdinalIgnoreCase)
.MessageEndsWith("null");
This is the most succinct and versatile way to initialize an ExpectedException that has expectations.
This example expects
- The exception to be an ArgumentException
- The exception message to
- start with "value"
- contain "cannot be" (case-insensitive)
- end with "null"
Implicit Conversion from exception type
ExpectedException expectedException = typeof(ArgumentException);
This is useful when only the exception type is required.
This example expects
- The exception to be an ArgumentException
Implicit Conversion from an exception instance
ExpectedException expectedException = new ArgumentException("value cannot be null");
If the source exception provided was previously thrown, then the value returned by expectedException.ToString() will be sourceException.ToString().
This example expects
- The exception to be an ArgumentException
- The exception message to
- equal "value cannot be null"
ExpectedException(Type exceptionType, params Expectation[] expectations) Constructor
ExpectedException expectedException = new ExpectedException(typeof(ArgumentException),
new MessageStartsWith("value"),
new MessageContains("cannot be", StringComparison.OrdinalIgnoreCase),
new MessageEndsWith("null"));
This is the verbose implementation of Implicit Conversion from Expect<TException>.
This example expects
- The exception to be an ArgumentException
- The exception message to
- start with "value"
- contain "cannot be" (case-insensitive)
- end with "null"
ExpectedException(Exception exception) Constructor
var exception = new ArgumentException("value cannot be null");
ExpectedException expectedException = new ExpectedException(exception);
This is the verbose implementation of Implicit Conversion from an exception instance. If the source exception provided was previously thrown, then the value returned by expectedException.ToString() will be sourceException.ToString().
This example expects
- The exception to be an ArgumentException
- The exception message to
- equal "value cannot be null"
Expectations
Expectations allow you to confirm a list of expected conditions are fulfilled by the exception.
MessageContains
Checks that the exception message contains a specific string value.
Parameter | Description | Restrictions |
---|---|---|
value | The string to seek. | Not null or empty |
comparisonType | One of the enumeration values that determines how the exception message and value are compared. | A valid System.StringComparison |
MessageContainsCount
Checks that the exception message contains a specific string value a specific number of times.
Parameter | Description | Restrictions |
---|---|---|
expectedCount | The expected number of instances of value within the exception message. | Greater than or equal to 0 |
value | The string to seek. | Not null or empty |
comparisonType | One of the enumeration values that determines how the exception message and value are compared. | A valid System.StringComparison |
MessageEndsWith
Checks that the exception message ends with a specific string value.
Parameter | Description | Restrictions |
---|---|---|
value | The string to compare to the substring at the end of the exception message. | Not null or empty |
comparisonType | One of the enumeration values that determines how the exception message and value are compared. | A valid System.StringComparison |
MessageEquals
Checks that the exception message equals a specific string value.
Parameter | Description | Restrictions |
---|---|---|
value | The string to compare to the exception message. | None |
comparisonType | One of the enumeration values that determines how the exception message and value are compared. | A valid System.StringComparison |
MessageMatches
Checks that the exception message matches a specific regular expression pattern.
Parameter | Description | Restrictions |
---|---|---|
pattern | The regular expression pattern to match. | Not null or empty and a valid regular expression |
options | A bitwise combination of the enumeration values that provide options for matching. | A valid System.Text.RegularExpressions.RegexOptions |
MessageStartsWith
Checks that the exception message starts with a specific string value.
Parameter | Description | Restrictions |
---|---|---|
value | The string to compare to the substring at the start of the exception message. | Not null or empty |
comparisonType | One of the enumeration values that determines how the exception message and value are compared. | A valid System.StringComparison |
ToString()
The value returned is different before and after comparing to a thrown exception.
Before expectedException.Equals(exception)
System.ArgumentException where
Message starts with "value"
Message contains "cannot be" (ComparisonType: OrdinalIgnoreCase)
Message ends with "null"
After expectedException.Equals(exception)
System.ArgumentException where
[+] Message starts with "value"
[-] Message contains "cannot be" (ComparisonType: OrdinalIgnoreCase)
[+] Message ends with "null"
[+] expectation matched; [-] expectation not matched
ResetResults()
Removes any previously cached results. The value returned by ToString() is reset to its initial value.
expectedException.ResetResults();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 167 | 7/23/2024 |
1.0.0-beta-3 | 96 | 7/23/2024 |
1.0.0-beta-2 | 75 | 7/23/2024 |
1.0.0-beta-1 | 77 | 7/23/2024 |
1.0.0-alpha | 76 | 7/22/2024 |