laradev / test-support

Laravel开发测试支持

dev-master / 0.1.x-dev 2017-05-27 19:30 UTC

This package is auto-updated.

Last update: 2024-09-13 14:52:30 UTC


README

本包提供辅助工具和实用程序,以简化Laradev包的测试。

安装

composer require --dev laradev/test-support

用法

创建测试用例

只需扩展类 Laradev\Test\Support\TestCase

namespace Example

use Laradev\Test\Support\TestCase;

final class ExampleClassTest extends TestCase
{
    /** implement abstract methods **/
} 

实用程序

TestCase 包含一系列以特性形式提供的实用程序。
虽然它们可以独立使用,但扩展 TestCase 类更为可靠。

断言

Assertions 特性通过向 $this 直接提供新的断言来增强 TestCase 类。

  • assertIsSubclassOf(string $expectedParent, string $actual, string $message = ''):断言实际类是给定类的一个子类。
  • assertBootMergesConfigForProvider(string $providerClass, string $configfile, string $message = ''):断言给定服务提供程序实例的 boot 方法将其包的配置与主应用程序配置合并。
  • assertBootPublishesConfigForProvider(string $providerClass, string $configfile, string $message = ''):断言给定服务提供程序实例的 boot 方法将配置文件发布到应用程序配置路径。

MockProvider

MockProvider 特性提供一组工厂方法,以简化对 Laravel 主类的模拟。

工厂方法

  • newMock($whatToMock = null):返回 $whatToMock 的实例,或如果参数值为 null,则返回 Mockery\MockInterface 的实例。
  • newAppMock():返回 Illuminate\Contracts\Foundation\Application 的实例。
  • newConfigMock():返回 Illuminate\Contracts\Config\Repository 的实例。
  • newFunctionMock(string $functionName):用于创建函数的模拟,它返回 Mockery\CompositeExpectation 的实例。
  • newAppContainerWithConfigMock():返回包含配置模拟实例的 Illuminate\Contracts\Container\Container 的实例,该实例可以通过键 config 访问。

由于底层的模拟引擎是 Mockery,所有这些实例都实现了 Mockery\MockInterfaceMockery\ExpectationInterface,然后可以通过期望进行增强。

其他方法

  • releaseMocks():释放所有模拟,进行一些清理。
  • useFunction(string $functionName, ...$args):静态方法,用于在模拟函数的体内调用(有关如何做到这一点,请参阅 MockProviderTest ::testMockingFunctions() 的示例)。

注意
在扩展抽象类 TestCase 时,releaseMocks 方法会在每个测试的 tearDown 方法结束时自动调用。
如果您打算直接使用 MockProvider 特性,请注意,您需要自己调用 releaseMocks 方法。

许可证

本项目根据 MIT 许可证 许可。