mcbates's avatar

Accessor does not render in Blade when running feature test

On my Note model, I have an Eloquent accessor getTypeDescriptionAttribute. It shows correctly in my UI, an unit test is passing, but the feature test that asserts that it sees the retrieved value fails.

Why does the accessor not work in the Blade when being rendered in the test? Any help on how to debug this?

Model:

public function getTypeDescriptionAttribute()
{
  return NoteType::getDescription($this->type) ?? 'Fallback';
}

Enum class:

final class NoteType extends Enum
{
    const Interaction =   0;
}

Blade (edit.blade.php)

@foreach ($notes as $note)
   <code>{{ $note }}</code>
   <li>{{ $note->type }}, {{ $note->type_description }}: {{ $note->body }}</li>
@endforeach

Unit test (green)

/** @test */
public function it_does_get_note_type_description()
{
 $note = factory(Note::class)->create();
 $this->assertEquals('Interaction', $note->type_description);
}

Feature test (red)

/** @test */
public function user_can_see_list_of_notes_for_contact_with_details()
{
  $this->withoutExceptionHandling();
  $contact = factory(Contact::class)->create();
  $note = factory(Note::class)->create();

  $response = $this->get(route('contact.show', $contact));
  $response->assertSee(e($note->body));
  $this->assertEquals('Interaction', NoteType::getDescription(0));
  $this->assertEquals(0, $note->type);
  $response->assertSee(NoteType::getDescription($note->type));
 }

Error message: https://gist.github.com/minthemiddle/b1fe67c0019ffcc5e448e8ee9378d7c4

UI:

0 likes
2 replies
mstrauss's avatar

@mcbates

Not sure if this helps, but if I am reading your code correctly, 0 is the $note->type and : Long sentence appears to be the $note->type_description in the output. Is it possible that in the below line of the test $note->type is being cast as a string as opposed to a int?

$response->assertSee(NoteType::getDescription($note->type));

1 like
mcbates's avatar

Thanks for helping, @mstrauss. Long sentence is $note->body, the empty space before : is where the description should go. But the blade does not render it.

Casting should not be the problem:

$description = NoteType::getDescription($note->type); // "Interaction"
$response->assertSee($description); // fails, because the string is not in the blade view
1 like

Please or to participate in this conversation.