简体   繁体   English

phpunit即使没有测试也给出错误(Laravel)

[英]phpunit gives errors even with no tests (Laravel)

Hi I just download phpunit.phar in my laravel project (which I already developed partly). 嗨,我刚刚在我的laravel项目中下载了phpunit.phar(我已经进行了部分开发)。 I started by doing a simple 我从做一个简单的开始

php phpunit.phar

but it outputs things like whole viewfiles with at the end: 但它输出的内容类似于整个viewfile,最后带有:

 {"error":{"type":"ErrorException","message":"Undefined variable: currency","file":"C:\\wamp\\www\\project.dev\\app\\views\\front\\settings.php","line":79}}

This is not even a test and the code works when testing out in the browser. 这甚至不是测试,并且在浏览器中进行测试时代码也可以工作。 I am also using the default phpunit configuration file shipped with laravel. 我还使用了laravel附带的默认phpunit配置文件。

EDIT: the code 编辑:代码

<label class="radio">
<input type="radio" name="settings_currency" id="set_curr_<?=$currency->id?>" value="<?=$currency->id?>" checked>
<span class="symbol"><?=$currency->symbol?></span>
<?=$currency->name?>
<span><?=$currency->abbr?></span>
</label>

You cannot simply run PHPUnit and expect it to test something useful. 您不能简单地运行PHPUnit并期望它测试有用的东西。 You have to configure it! 您必须配置它!

When run, PHPUnit scans the current directory and all subdirectories for PHP files, includes them and checks if there are any test classes defined that can be run. 运行时,PHPUnit扫描当前目录和所有子目录中的PHP文件,包括它们并检查是否定义了可以运行的任何测试类。 Including your view files will execute them without context, ie you'll get the complete echo and notices from any used, but currently undefined variables. 包括您的视图文件将在没有上下文的情况下执行它们,即,您将从所有已使用但当前未定义的变量中获得完整的回显和通知。

As a starter, create a "phpunit.xml" in the same directory you are starting PHPUnit. 首先,在与启动PHPUnit相同的目录中创建一个“ phpunit.xml”。 Read the documentation on how to create it's contents. 阅读有关如何创建其内容的文档 First and foremost you need to specify which directory to scan for test classes with the <testsuites> element. 首先,您首先需要使用<testsuites>元素指定要扫描哪个目录的测试类。

the code works when testing out in the browser. 该代码在浏览器中测试时有效。

Well, as you might can imagine, only having some "code working in the browser" must not mean the code is error-free. 就像您可以想象的那样,仅具有一些“在浏览器中运行的代码”并不意味着该代码没有错误。

See the error message again you get: 再次看到错误消息,您得到:

Undefined variable: currency 未定义的变量:货币

So what you experience here can be explained shortly as: PHPUnit is more strict with your code than you are. 因此,您在这里的经历可以简单地解释为:PHPUnit对您的代码的要求比对您的要求更为严格。 It will mark warnings you didn't care about in the past, like in this example Undefined Variables . 它将标记您过去不关心的警告,例如本示例中的Undefined Variables

So now learn about how to define variables and why using Undefined Variables is certainly your Problem and then you will easily understand as well why PHPUnit checks for those. 因此,现在了解如何定义变量以及为什么使用未定义变量无疑是您的问题 ,然后您将很容易理解为什么PHPUnit会检查这些变量

You can as well lower your expectations by lowering the quality standards PHPUnit applies when running tests by configuring it: Appendix C. The XML Configuration File 您还可以通过配置PHPUnit降低质量,从而降低期望值,PHPUnit在运行测试时会对其进行配置: 附录C。XML配置文件

However, with the default config, it's just giving you that error and you should just fix it. 但是,使用默认配置,它只是给您该错误,您应该对其进行修复。 PHPUnit does not only run tests, it also executes some code and deals with issues related to executing code, like getting Warnings, Errors and Notices. PHPUnit不仅运行测试,还执行一些代码并处理与执行代码有关的问题,例如获取警告,错误和通知。

It's you who need to deal with that if you execute phpunit. 如果执行 phpunit,则需要您处理。 Otherwise, just step back and do not execute it :) 否则,请退后一步,不要执行它:)

I am also using the default phpunit configuration file shipped with laravel. 我还使用了laravel附带的默认phpunit配置文件。

Yes you are. 是的,你是。 That is why tests are run. 这就是为什么要运行测试。 And that's why you're caught in the act of using Undefined Variables . 这就是为什么您会陷入使用未定义变量的行为的原因。


See as well: 另请参阅:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM