Laravel Mock进行Unitari测试功能

时间:2020-08-25 07:06:05

标签: php laravel unit-testing mocking phpunit

我是模拟的新手,我无法找出使用模拟/存根单一测试功能的方法。

这是我的测试,我要检查剪贴对象中的getCourse方法是否将返回我作为参数传递给函数的特定过程的数据。

    public function test_get_detailed_info_from_PHPcourse()
    {
        $phpCourse = new Course('PHP');
        $result = $this->scrapy->getCourse($phpCourse);

        $this->assertContains('PHP Tutorial',$result);
        $this->assertContains('100',$result);
        $this->assertContains('260 XP',$result);
    }

getCourse函数中的代码如下:

public function getCourse(Course $targetCourse)
    {
        $all_courses = $this->getAllCourses();
        foreach ($all_courses as $course){

            if (in_array ($targetCourse->getName(), $course)){
                return $course;
            }
        }
    }

我嘲笑了辅助函数getAllCourses(),该函数用于在getCourse()函数内部抓取所有课程。

$scrapper = $this->mock(SoloLearnScraping::class, function ($mock) {
            $allCourses = include 'tests/Unit/Mock_CoursesSoloLearn.php';
            $mock->shouldReceive('getAllCourses')->andReturns($allCourses);
        });

我的问题是:如何在给定php课程的情况下进行测试,getCourse()将返回来自php课程的数据,并在html课程中传递时输出html课程数据?

0 个答案:

没有答案