本文介绍了PHPUnit测试套件-无法重新声明类Mocking&具体课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.

我有一个测试套件,正在测试几个类.我的所有班级都使用依赖项注入.

I have a test suite that is testing a few classes. My classes all use dependency injection.

我有一个名为scheduleHandler的类,它可以通过所有测试.然后我的另一个类ruleHandler有一个需要scheduleHandler实例的方法.我不想传递真实的scheduleHandler,所以我尝试创建一个模拟的scheduleHandler进行注入.

I have a class called scheduleHandler that passes all tests. Then my other class ruleHandler has a method that requires an instance of scheduleHandler. I dont want to pass in the real scheduleHandler so I tried to create a mock scheduleHandler to inject in.

我的问题是,因为scheduleHandler类是在RuleHandler之上的套件中进行测试的,所以当创建模拟时,我得到:-

The problem I have is that because the scheduleHandler class is tested in the suite above ruleHandler, when the mock is created I get:-

PHP Fatal error:  Cannot redeclare class scheduleHandler

如果我不使用测试套件,并单独运行测试,一切都很好.

If I dont use a test suite, and run the tests individually everything is fine.

有人知道解决这个问题的方法吗?

Anyone know of a way to get round this ?

推荐答案

到目前为止我最好的猜测:

My best guess so far:

var_dump(class_exists('scheduleHandler', false));

为您返回false.这意味着该类尚不存在.现在,如果您在phpunit尝试从中自动加载程序时找不到该类,则phpunit会创建它自己的类.

returns false for you. That means the class doesn't exist yet. Now if you autoloader doesn't find the class when phpunit is trying to extend from it phpunit will create the class it's self.

如果您后来又走了,那么要求REAL类从那些地方碰撞到类.

If you later down the road then require the REAL class from somewhere those to classes will collide.

要对此进行测试,请确保在创建模拟对象之前已要求使用REAL scheduleHandler类.

To test this make sure you have required your REAL scheduleHandler class BEFORE creating the mock object.

这篇关于PHPUnit测试套件-无法重新声明类Mocking&具体课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:03