fluent-asserts 0.6.3

Fluent assertions done right


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Build Status DUB Version DUB Installs Percentage of issues still open Average time to resolve an issue

Writing unit tests is easy with Dlang. The unittest block allows you to start writing tests and to be productive with no special setup.

Unfortunately the assert expresion does not help you to write expressive asserts, and in case of a failure it's hard to find why an assert failed. The fluent-asserts library allows you to more naturally specify the expected outcome of a TDD or BDD-style test.

To begin

  1. Add the DUB dependency: https://code.dlang.org/packages/fluent-asserts

  2. Import it:

import fluent.asserts;
  1. Use it:
    unittest {
        true.should.equal(false).because("this is a failing assert");
    }

    unittest {
        Assert.equal(true, false, "this is a failing assert");
    }
  1. Run the tests:
➜  dub test --compiler=ldc2

asciicast

API Docs

The library provides the should template and the Assert struct.

Should

should can be used in combination with Uniform Function Call Syntax (UFCS)

auto should(T)(lazy const T testData);

So the following statements are equivalent

testedValue.should.equal(42);
should(testedValue).equal(42);

In addition, the library provides the not and because modifiers that allow to improve your asserts.

not negates the assert condition:

testedValue.should.not.equal(42);

because allows you to add a custom message:

    true.should.equal(false).because("of test reasons");
    ///will output this message: Because of test reasons, true should equal `false`.

Assert

Assert is a wrapper for the should struct that allows you to use the asserts with a different sintax.

For example, the folowing lines are equivalent:

    testedValue.should.equal(42);
    Assert.equal(testedValue, 42);

All the asserts that are available using the should syntax are available with Assert. If you want to negate the check, just add not before the assert name:

    Assert.notEqual(testedValue, 42);

You can use fluent asserts with:

Do you already have a lot of tests?

If you want to get the failure location for failing tests written using the DLang's assert you can use the fluent assert handler which will add extra information to the default assert message.

    shared static this() {
        import fluent.asserts;
        setupFluentHandler;
    }

License

MIT. See LICENSE for details.

Authors:
Szabo Bogdan
Sub packages:
fluent-asserts:core, fluent-asserts:vibe
Dependencies:
fluent-asserts:core
Versions:
0.7.1 2017-Nov-03
0.7.0 2017-Oct-01
0.6.7 2017-Sep-21
0.6.6 2017-Aug-15
0.6.5 2017-Aug-04
Show all 22 versions
Download Stats:
  • 16 downloads today

  • 159 downloads this week

  • 1222 downloads this month

  • 6783 downloads total