Expectable 1.0.0-alpha

This is a prerelease version of Expectable.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Expectable --version 1.0.0-alpha                
NuGet\Install-Package Expectable -Version 1.0.0-alpha                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Expectable" Version="1.0.0-alpha" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Expectable --version 1.0.0-alpha                
#r "nuget: Expectable, 1.0.0-alpha"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Expectable as a Cake Addin
#addin nuget:?package=Expectable&version=1.0.0-alpha&prerelease

// Install Expectable as a Cake Tool
#tool nuget:?package=Expectable&version=1.0.0-alpha&prerelease                

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

  1. Define an ExpectedException with an exceptionType and optional list of expectations.

    // using Expectable;
    
    ExpectedException expectedException = Expect<ArgumentException>.Where
        .MessageStartWith("value")
        .MessageContains("cannot be", StringComparison.OrdinalIgnoreCase)
        .MessageEndsWith("null");
    
  2. Catch exception

    var value = default(string);
    var exception = Assert.Throws(expectedException.Type, () => DoSomething(value));
    
    // void DoSomething(string value)
    // {
    //      if (value == null) 
    //         throw new ArgumentException("value is null");
    //     ...
    // }
    
  3. Compare exception

    Assert.Equal(expectedException, exception);
    

    when not equal

    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)
    

Expectations

Expectations allow you to confirm an optional list of expected conditions are fulfilled by the exception.

MessageContains

Checks that the exception message contains a specific string value.

  • value : The string to seek.
  • comparisonType : One of the enumeration values that determines how the exception message and value are compared. (Default is StringComparison.Ordinal)

MessageContainsCount

Checks that the exception message contains a specific string value a specific number of times.

  • expectedCount : The expected number of instances of value within the exception message.
  • value : The string to seek.
  • comparisonType : One of the enumeration values that determines how the exception message and value are compared. (Default is StringComparison.Ordinal)

MessageEndsWith

Checks that the exception message ends with a specific string value.

  • value : The string to compare to the substring at the end of the exception message.
  • comparisonType : One of the enumeration values that determines how the exception message and value are compared. (Default is StringComparison.Ordinal)

MessageEquals

Checks that the exception message equals a specific string value.

  • value : The string to compare to the exception message..
  • comparisonType : One of the enumeration values that determines how the exception message and value are compared. (Default is StringComparison.Ordinal)

MessageMatches

Checks that the exception message matches a specific regular expression pattern.

  • pattern : The regular expression pattern to match.
  • options : A bitwise combination of the enumeration values that provide options for matching. (Default is RegexOptions.None)

MessageStartsWith

Checks that the exception message starts with a specific string value.

  • value : The string to compare to the substring at the start of the exception message.
  • comparisonType : One of the enumeration values that determines how the exception message and value are compared. (Default is StringComparison.Ordinal)

ToString()

The value returned is different before and after comparing to a thrown exception. A

Before expectedException.Equals(thrownException)

System.ArgumentException where
      Message starts with "value"
      Message contains "cannot be" (ComparisonType: OrdinalIgnoreCase)
      Message ends with "null"

After expectedException.Equals(thrownException)

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();

Initialization Alternatives

While using Expect<TException>.Where ... is the most succinct and versatile way to initialize an ExpectedException with expectations, the following alternatives are also available.

Constructor ExpectedException(Type exceptionType, params Expectation[] expectations)

ExpectedException expectedException = new ExpectedException(typeof(ArgumentException),
    new MessageStartsWith("value"),
    new MessageContains("cannot be", StringComparison.OrdinalIgnoreCase),
    new MessageEndsWith("null"));

This is the verbose implementation of Expect<TException>.

Constructor ExpectedException(Exception exception)

ExpectedException expectedException = new ExpectedException(new ArgumentException("value cannot be null"));
// expectedException.Type: typeof(ArgumentException)
// expectedException.Expectations: MessageEquals("value cannot be null")

If the source exception provided was previously thrown, then the value returned by expectedException.ToString will be sourceException.ToString()

Implicit Conversion from an exception instance

ExpectedException expectedException = new ArgumentException("value cannot be null");
// expectedException.Type: typeof(ArgumentException)
// expectedException.Expectations: MessageEquals("value cannot be null")

If the source exception provided was previously thrown, then the value returned by expectedException.ToString will be sourceException.ToString()

Implicit Conversion from exception type

ExpectedException expectedException = typeof(ArgumentException);
// expectedException.Type: typeof(ArgumentException)
// expectedException.Expectations: empty
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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 104 7/23/2024
1.0.0-beta-3 64 7/23/2024
1.0.0-beta-2 53 7/23/2024
1.0.0-beta-1 55 7/23/2024
1.0.0-alpha 57 7/22/2024