Pest: RFC: add `expect` function

Created on 28 Jun 2020  路  7Comments  路  Source: pestphp/pest

When we are writing tests, we often need to check that values meet certain conditions. And for that, at the time of this writing, we are using PHPUnit syntax using the global helpers: assertEquals, assertTrue, assertFalse, etc.

The problem with this is the number of global helpers that are injected in the global namespace: One function per assertion.

This RFC introduces the global helper expect, that would offer the same number of assertions of PHPUnit, but using a single global function and a fluent syntax like so:

// old
assertTrue($variable);
// new
expect($variable)->toBe(true);

// --

// old
assertGreaterThan(0, $variable);
// new
expect($variable)->toBeGreaterThan(0);

// --

// old
assertContains('nuno', $array);
// new
expect($array)->toContain('nuno');

Of course, the RFC is inspired in Jest: jestjs.io/docs/en/expect.

enhancement

Most helpful comment

Just plug this in and you are ready! 馃幆
https://github.com/Codeception/Verify

All 7 comments

@nunomaduro for easier migration and reduce cognitive load, can the default expectations be consistent with the asserts then add aliases to better sounding or user friendly names

hhvm has some good libraries that can inspire: FBExpect and HackTest

Just plug this in and you are ready! 馃幆
https://github.com/Codeception/Verify

In progress...

Will they be chainable? For example:

expect($array)->toContain('nuno')->toHaveMinElements(5)->toIncludeElementsFrom($array2);

Like Laravel's Validation system. And maybe have clauses like toContainWhen('nuno', count($array) > 0).

I've just bootstrapped this work of adding the full-featured expect API.

Check out the pull request: https://github.com/pestphp/pest/pull/123. Fell free to help me with this. 馃憤馃徎

@tpaksu Yes: https://github.com/pestphp/pest/pull/123/files#diff-a644964bd3325a15734d76efdaabd137R52.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gulien picture gulien  路  3Comments

albertpratomo picture albertpratomo  路  7Comments

olivernybroe picture olivernybroe  路  7Comments

nunomaduro picture nunomaduro  路  5Comments

zingimmick picture zingimmick  路  3Comments