JackJones's avatar

Trying to learn Mockery - "This test did not perform any assertions"

I'm trying to test that my class is correctly applying orderBy()'s

I have a test like this:

If I change ->with('name', 'asc') to ->with('name', 'ascxyz') I get an ugly Exception from Mockery:

Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_0_Illuminate_Contracts_Database_Query_Builder::orderBy('name', 'asc'). Either the method was unexpected or its arguments matched no expected argument list for this method

If I change ['field' => 'age', 'order' => -1], to anything else ['field' => 'age', 'order' => 'somevalue'], then it makes literally no difference?

I'm obviously completely new to this lol

0 likes
5 replies
tisuchi's avatar

Hello @jackjones

Your test is currently not making any explicit assertions because Mockery automatically checks if the expected methods were called with the correct arguments. Mockery doesn't need an assertion if the expectations are met because the test passes silently. If the expectations aren't met, it throws an exception, which is why you see the NoMatchingExpectationException.

Have you checked this docs? https://laravel.com/docs/11.x/mocking

JackJones's avatar

@tisuchi How do I make the test pass in that case? assertTrue(true)?

My method is pretty simple:

    public function sort(Builder $query, array $sort, array $exceptions = [], array $substitutes = []): void
    {
        foreach ($sort as $s) {
            if (in_array(strtolower($s['field']), $exceptions)) {
                continue;
            }

            $s['field'] = $substitutes[$s['field']] ?? $s['field'];

            $query->orderBy($s['field'], $s['order'] === 1 ? 'asc' : 'desc');
        }
    }

I can change the expectations to anything I like and Mockery still doesn't report anything, for example:

$query->shouldNotReceive('orderBy')
            ->with('age', 'desc')
            ->once();
$query->shouldReceive('orderBy')
            ->with('age', 'desc')
            ->once();

One of them must logically fail, but it doesn't?

tisuchi's avatar

@JackJones

... assertTrue(true)?

This is one of the ways to confirm (if you wish) because mockery asserts will check the 'orderBy' calls.

Here I provide you a simple example:


$mock = Mockery::mock('Foo');
$mock->shouldReceive('bar')->with('baz')->once();
$mock->bar('baz'); // This will pass

$mock->bar('something else'); // This will fail because it doesn't meet the expectation
JackJones's avatar

Is it normal for $this->app to be null? I was watching one of Jeff's videos and it said you could use the likes of $this->app->instance

    public function test_sort_method_applies_order_by_correctly()
    {
        dd($this->app);

app is null in the console

Edit: I didn't have parent setup in the setup method, oops

Edit 2: by adding the use MockeryPHPUnitIntegration; trait, it counts the Mockery tests as assertions, so no need to use assertTrue(true)

tisuchi's avatar

@JackJones Can you please provide your full implementation FilterService and how do you want to call in your query/use?

Please or to participate in this conversation.