简体   繁体   English

使用PHPUnit在PHP中测试REST API控制器

[英]Testing REST API controller in PHP with PHPUnit

I have a very small test suite for testing CRUD operations on a REST API. 我有一个非常小的测试套件,用于测试REST API上的CRUD操作。 My controller displays a form, table, etc, based on the information provided by the API. 我的控制器根据API提供的信息显示表单,表格等。 My test class create a new Buzz object and makes requests to the API server. 我的测试类创建一个新的Buzz对象,并向API服务器发出请求。

Unfortunately, this sucks, because if i update or delete something, it is deleted in the API. 不幸的是,这很糟糕,因为如果我更新或删除某些内容,则会在API中将其删除。 This is a very bad test practice. 这是非常糟糕的测试做法。 How can i unit test my application, when using an external data source like this? 使用这样的外部数据源时,如何对应用程序进行单元测试?

Look into test doubles and stubs in the PHPUnit docs: 在PHPUnit文档中查看测试双打和存根:

http://www.phpunit.de/manual/current/en/test-doubles.html http://www.phpunit.de/manual/current/en/test-doubles.html

You can use them to replace API functionality. 您可以使用它们来替换API功能。

Unfortunately, this sucks, because if i update or delete something, it is deleted in the API. 不幸的是,这很糟糕,因为如果我更新或删除某些内容,则会在API中将其删除。 This is a very bad test practice. 这是非常糟糕的测试做法。 How can i unit test my application, when using an external data source like this? 使用这样的外部数据源时,如何对应用程序进行单元测试?

Create some fake, erasable data.. matter of fact you should be not even running PHPUnit tests on a production database/server. 创建一些伪造的,可擦除的数据..事实上,您甚至不应该在生产数据库/服务器上运行PHPUnit测试。

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

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