FvsJson's avatar

Unit test escapeWhenCastingToString issue

Hi all im writing a unit test to check that the view has the collection data

    #TEST
    $appliedUsers = AppliedJobPosts::query()
        ->where('job_posts_id', $jobPost->id)
        ->with('user', function ($query) {
            $query->select('id', 'name', 'surname');
        })
        ->latest('created_at')
        ->limit(5)
        ->get();

    Livewire::actingAs($user)
        ->test(UserJobSearchAppliedUsers::class, ['jobPostId' => $jobPost->id])
        ->assertViewHas('lastAppliedUsers', $appliedUsers);

# view code

@foreach($lastAppliedUsers as $appliedUser)

            <x-job-seeker.user-job-seeker-applied-user-list-item
                :applied-user="$appliedUser"
                :is-not-odd="!$loop->odd"
            />

        @endforeach

However im getting a Failed asserting that two objects are equal. as a result that expected is 'escapeWhenCastingToString' => false and the actual is 'escapeWhenCastingToString' => true...

Why is this happening and how can i fix it or correct the test?

0 likes
5 replies
Shivamyadav's avatar

whole running your test what it gives you error? can you show it pealse!

FvsJson's avatar

@Shivamyadav below is the error

Failed asserting that two objects are equal. <Click to see difference>

/home/vagrant/projects/snp-portal-solidarity/vendor/livewire/livewire/src/Testing/Concerns/MakesAssertions.php:366 /home/vagrant/projects/snp-portal-solidarity/tests/Feature/JobSeeker/UserJobSearchVacancySummaryTest.php:300

Sinnbeck's avatar

You are asserting on the view, not blade code. So show the livewire class

FvsJson's avatar

@Sinnbeck

class UserJobSearchAppliedUsers extends Component
{
    protected JobSeekerService $jobPostService;

    public $jobPostId;

    public function boot(JobSeekerService $jobSeekerService)
    {
        $this->jobPostService = $jobSeekerService;
    }

    public function mount($jobPostId)
    {
        $this->jobPostId = $jobPostId;
    }

    public function render()
    {

        $lastAppliedUsers = $this->jobPostService->getLastAppliedJobPostApplicants($this->jobPostId);

        return view('livewire.user-job-search.tabs.user-job-search-applied-users',
            compact('lastAppliedUsers')
        );
    }
}
FvsJson's avatar

So i updated my test as below and now im getting a conver to int issue

#Updated test
Livewire::actingAs($user)
        ->test(UserJobSearchAppliedUsers::class, ['jobPostId' => $jobPost->id])
        ->assertViewHas('lastAppliedUsers', function ($lastAppliedUsers) use ($appliedUsers) {
            return !$lastAppliedUsers->contains($appliedUsers);
        });

New error:

ErrorException : Object of class Illuminate\Database\Eloquent\Collection could not be converted to int

Please or to participate in this conversation.