Adam_Commit's avatar

:assertCount() must be a countable or iterable - Laravel 7

Hey everyone,

Been trying to battle away with the Lets build a Forum with TDD, and I am about half way through, so far, its going OK, but been stuck for a while now on why I can't get my tests to pass.

Basically - all test are passing apart from this one:

    public function test_a_notification_is_prepared_when_a_subscribed_thread_receives_a_new_reply_that_is_not_by_current_user()
    {

        $thread = create('App\Thread')->subscribe();

        $this->assertCount(0, auth()->user()->notifications);

        $thread->addReply([
            'user_id' => auth()->id(),
            'body' => 'Some reply here'
        ]);

        $this->assertCount(0, auth()->user()->fresh()->notifications);

        $thread->addReply([
            'user_id' => create('App\User')->id,
            'body' => 'Some reply here'
        ]);

        $this->assertCount(1, auth()->user()->fresh()->notifications);

    }

With the error in the test being

Argument #2 of PHPUnit\Framework\Assert::assertCount() must be a countable or iterable

I have commented out all 3 instances of this, and tried to debug it, but can't work it out. All other test are passing, I have also tried to look at the Github repo for the corresponding episodes and my code is almost identical - in fact I tried the code from the repo to double check I was being silly.

Has anyone come across this issue? The episodes are around 44-46 of the series "Lets build a forum with TDD"

I am building on Laravel 7 with PHP 7.4. The PHP unit version is 8.5.8.

I am not bothered at this stage if the test does not pass, I just want to change the error message!

Cheers

0 likes
5 replies
tykus's avatar

auth()->user()->fresh()->notifications must be null rather than an empty Collection; how is notifications relationshop defined on the User class?

Adam_Commit's avatar

Thanks @tykus it's defined using the notifiable trait pretty much out of the box as per the docs, and called via ->notify method

tykus's avatar
tykus
Best Answer
Level 104

Ok can you dump(auth()->user()) and dd(auth()->user()->notifications to see what you are getting?

Adam_Commit's avatar

Hmmmm, I think it could be a conflict looking at those specific dumps. I currently have a field of the user model called notifications that is a boolean 1/0 column that I use to flag whether a user has accepted to receive notifications in the registration.

Do you think it is possible that the test is actually conflicting with that field name as the DD on

dd(auth()->user()->notifications);

Return Int 1 - which I believe is referring to the col value on the user table as opposed to the notification collection?

I will try modifying the name of that column and run the tests again.

Adam_Commit's avatar

Cheers @tykus - To confirm - conflict between user table column naming conversion and the notification collection/relationship on the user model - changing the notification column flag to allow_notifications changed the dump from an integer to a collection - and therefore, was countable, thus allowing me to use the assertCount.

Thanks

Adam

Please or to participate in this conversation.