phplaravelunit-testing

BadMethodCallException: Call to undefined method Database\Factories\PulseAnswerFactory::getRelated()


Getting this error with a unit test

1) DeletePulseMutationTest::testDeletePulseDeletesCorrectResources
BadMethodCallException: Call to undefined method Database\Factories\PulseAnswerFactory::getRelated()

Here is the code that is failing in my unit test

Pulse::factory()
    ->create([
        'pulse_period_start_date' => '2021-01-01',
        'pulse_period_end_date' => '2021-01-31',
        'submitted_by_id' => $submitter,
        'reviewed_by_id' => $reviewer,
        'reviewed_at' => '2021-02-01'
    ])
    ->has(
        PulseAnswer::factory()
            ->count(1)
            ->has(
                PulseReply::factory()
                    ->count(1)
            )
    );

I'm using Laravel version 11.28 and following these docs https://laravel.com/docs/11.x/eloquent-factories#factory-relationships

Here are the models and their relations

    class Pulse extends Model
    {
    
        use SoftDeletes;
        use HasFactory;
    
        protected $fillable = [
            'pulse_period_start_date',
            'pulse_period_end_date',
            'submitted_by_id',
            'reviewed_by_id',
            'reviewed_at'
        ];
    
        /**
         * @codeCoverageIgnore
         */
        public function answers()
        {
            return $this->hasMany(PulseAnswer::class);
        }
    class PulseAnswer extends Model
    {
    
        use SoftDeletes;
        use HasFactory;
    
        protected $fillable = [
            'pulse_id',
            'question_id',
            'answer',
            'additional_context'
        ];
   
    
        /**
         * @codeCoverageIgnore
         */
        public function replies()
        {
            return $this->hasMany(PulseReply::class);
        }
    }
    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    
    class PulseReply extends Model
    {
    
        use SoftDeletes;
        use HasFactory;
    
        protected $fillable = [
            'pulse_answer_id',
            'parent_reply_id',
            'reply',
            'reply_written_by_id'
        ];
    
        public function parent_reply()
        {
            return $this->belongsTo(__CLASS__, 'parent_reply_id', 'id');
        }
    
        public function answer()
        {
            return $this->belongsTo(PulseAnswer::class, 'pulse_answer_id');
        }
    
        public function reply_written_by()
        {
            return $this->belongsTo(User::class, 'reply_written_by_id');
        }
    }

A pulse has many pulse answers and a pulse answer has many replies.

I'm unsure as to why the relationships are not working properly.


Solution

  • The problem is you're calling has after create.

    Pulse::factory()
        ->has(
            PulseAnswer::factory()
                ->count(1)
                ->has(
                    PulseReply::factory()
                        ->count(1)
                )
        )
        ->create([
            'pulse_period_start_date' => '2021-01-01',
            'pulse_period_end_date' => '2021-01-31',
            'submitted_by_id' => $submitter,
            'reviewed_by_id' => $reviewer,
            'reviewed_at' => '2021-02-01'
        ]);