Skip to content

Provides a mechanism for a static import of NUnit Expect() syntax (deprecated: see https://github.com/fluffynuts/NExpect for the next generation)

License

Notifications You must be signed in to change notification settings

fluffynuts/NUnit.StaticExpect

Repository files navigation

DEPRECATED

Please consider https://github.com/fluffynuts/NExpect as an alternative, better in many respects:

  • more fluent syntax
  • easier to extend
  • deep equality checking
  • existing extension packages for NSubstitute and AspNetCore route / verb testing
  • active development and usage -- I use NExpect daily

... on with the prior README ...

NUnit.StaticExpect

Some users prefer a shorter form of assertion than is given by NUnit's Assert.That. If you statically import NUnit.StaticExpect, the Expect() method may be used instead...

Usage

  1. Install the nuget package (install-package nunit.staticexpect)
  2. Import the static methods: add the following using statement to your test class file:
// C#
using static NUnit.StaticExpect.Expectations;
' VB
Imports NUnit.StaticExpect.Expectations
  1. Use the Expect() syntax:
[Test]
public void Expect_True_IsTrue()
{
  Expect(bool condition);
  Expect(bool condition, string message, params object[] parms);

  Expect(ActualValueDelegate del, IResolveConstraint constraint)
  Expect(ActualValueDelegate del, IResolveConstraint constraint,
        string message, params object[] parms)

  Expect<TActual>(TActual actual, IResolveConstraint constraint)
  Expect<TActual>(TActual actual, IResolveConstraint constraint,
                  string message, params object[] parms)

  Expect(TestDelegate del, IResolveConstraint constraint);
}

In addition, NUnit.StaticExpect allows the test fixture where the static import has been done to make direct use of many of the syntactic elements that would normally require you to specify the Is, Has or Does classes in order to use them. For example, you can write...

Expect(actual, EqualTo("Hello"));

NUnit.StaticExpect depends on NUnit -- indeed, installing the NUnit.StaticExpect package should also install the relevant NUnit package if not already installed. As such, all NUnit syntax is available, including Assert.Warn and Assume.That. Most of NUnit.StaticExpects methods and properties are simply pass-through shims to the relevant NUnit ones.

Why does this project exist?

NUnit has historically had a class named AssertionHelper which your test fixtures could inherit to provide the above syntax. NUnit issue #1212 discusses the deprecation of AssertionHelper.

Personally, I had been using AssertionHelper for about 6 months before I saw it marked as deprecated after an NUnit upgrade. NUnit.StaticExpect has been made to provide the same functionality for anyone who doesn't want to retrofit existing tests to use the newer Assert.That syntax or for anyone who just plain prefers the Expect() syntax.

Missing something?

NUnit.StaticExpect has a suite of tests to prove parity with AssertionHelper. Still, the contributors are only human, so we may have missed something. If this library is missing something you're used to from AssertionHelper, log an issue (: Pull requests are also welcome.

About

Provides a mechanism for a static import of NUnit Expect() syntax (deprecated: see https://github.com/fluffynuts/NExpect for the next generation)

Resources

License

Stars

Watchers

Forks

Packages

No packages published