在Job Class中测试功能

问题描述:

I like to test (Feature test via phpunit) some methods in the Job Class (Lumen Queue) to make sure it is working correctly.

Problem is Job Class have some jobs methods like $this->job->getJobId() and $this->release(5);

If I run phpunit from console, I get an error:

Error: Call to a member function getJobId() on null

The test code look like this:

/**
 * @test
 */
public function it_has_successfully_uploaded()
{        
    $job = new SomeJob(['file' => ['file1.zip']]);

    $job->handle();
}

How do I solve this issue?

Your code is using $this->job->getJobId(), but nowhere is that property declared or set. Are you perhaps using the InteractsWithQueue trait but forgot to include that in your code paste?

If so, the job property is set in InteractsWithQueue::setJob. This method may be called from several places, but in your case it is probably from Illuminate\Queue\CallQueuedHandler::setJobInstanceIfNecessary. This is some internal initialization of your job that Laravel does for you, which you must imitate in your test setup.

I would implement a simplistic version of Illuminate\Contracts\Queue\Job and call $job->setJob(new SimplisticVersionShazaamJob(...)); before calling $job->handle().