Laravel 4 codeception-模拟api调用数据
我正在为一个前端应用程序构建和编写测试,该应用程序正在为其所有数据调用API。我正在用Codeception进行测试。到目前为止,功能测试和验收测试仍然有效。但是,我希望功能测试独立于API,这样我就可以在不依赖API服务应用程序的情况下运行它们Laravel 4 codeception-模拟api调用数据,laravel-4,codeception,Laravel 4,Codeception,我正在为一个前端应用程序构建和编写测试,该应用程序正在为其所有数据调用API。我正在用Codeception进行测试。到目前为止,功能测试和验收测试仍然有效。但是,我希望功能测试独立于API,这样我就可以在不依赖API服务应用程序的情况下运行它们 有没有办法模拟来自API调用的数据?或者这是单元测试的领域 我使用PHPUnit进行API测试。我希望这对你有帮助 我刚刚为这个测试提供了一些示例输入数据,并验证它是否会像预期的那样返回错误/成功代码 如果测试没有得到预期的返回代码,那么它将提示一个错
有没有办法模拟来自API调用的数据?或者这是单元测试的领域 我使用PHPUnit进行API测试。我希望这对你有帮助 我刚刚为这个测试提供了一些示例输入数据,并验证它是否会像预期的那样返回错误/成功代码 如果测试没有得到预期的返回代码,那么它将提示一个错误
class ForgotPasswordTest extends \TestCase{
/**
* Test Forgot Password API With valid parameters
* It will check forgot password functionality with success conditions
*/
public function testForgotPasswordWithValidParameter()
{
$param=array("email"=>"shindesatishsss@gmail.com");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 600 in case if API executed successfully
*/
$this->assertEquals("600", $data["code"]);
}
/**
* Test Forgot Password API With Invalid parameters
* It will check whether you have applied user exist condition
*/
public function testForgotPasswordWithInValidParameter()
{
$param=array("email"=>"test@test.com");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 404 if user not found
*/
$this->assertEquals("404", $data["code"]);
}
/**
* Test Forgot Password API With Invalid parameters
* It will check input validations are working fine in the API
*/
public function testForgotPasswordWithInValidEmail()
{
$param=array("email"=>"satish");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 400 error code if there are some input validation error
*/
$this->assertEquals("400", $data["code"]);
}
}
您还可以设置一些其他的测试用例,只需要在这个类中使用不同的测试用例创建新函数。请查看Mocky(),了解您可以自定义的实时模拟HTTP响应。另外,一个创建非常棒的存根JSON对象的好工具,请查看JSON生成器()。这可能取决于您使用什么REST库来进行这些API调用。否则,与框架和库无关的解决方案将是创建一个轻量级服务器进程来模拟API响应,并在测试时使用该服务器而不是真正的API服务器