vendredi 11 octobre 2019

Laravel problem with testing when using astrotomic/laravel-translatable

I've got a model which uses translations

class Category extends Model
{
    use Translatable, SoftDeletes;

    public $translatedAttributes = ['name', 'description'];
    protected $fillable = [];

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    protected $with = ['translations'];

    public function getShortDescriptionAttribute()
    {
        return Str::limit($this->description, 30, '...');
    }
}

I am writing a test to see whether I print the name and short description of the category when the category listing page is visited.

class CategoriesTest extends TestCase
{
    use RefreshDatabase;

    /**
     * A basic feature test example.
     *
     * @test
     * @return void
     */
    public function categories_listing()
    {
        $category = factory(Category::class)->create();

        $response = $this->get(route('categories.index'));

        $response->assertStatus(200)
            ->assertSee($category->name)
            ->assertSee($category->shortDescription);
    }
}

I still do not print them, but the test return true. I checked why and it seems that the translated values of the category return null. I tried dd-ing the $category variable and it seems like it cannot load the translations relation. Everything works fine when I serve my app on the browser. There is not a problem in the factory or something else.

Aucun commentaire:

Enregistrer un commentaire