【发布时间】:2019-05-02 14:32:21
【问题描述】:
我想在我的存储库中为 save 方法编写 phpunit 测试。我的回购代码是:
public function saveCustomer(Custom $custom)
{
try
{
$custom->save();
return array(
'status' => true,
'customerId' => $custom->getId()
);
}
catch(\Exception $e)
{
return array(
'status' => false,
'customerId' => 0
);
}
}
我写了这个测试:
public function testSaveNewUye()
{
$request = array(
'email' => 'www@www.com',
'phone' => '555 555 555',
'password' => '34636'
);
$repo = new CustomerRepository();
$result_actual = $this->$repo->saveCustomer($request);
$result_expected = array(
'status' => true,
'customerId' => \DB::table('custom')->select('id')->orderBy('id', 'DESC')->first() + 1
);
self::assertEquals($result_expected, $result_actual);
}
我收到以下错误:
ErrorException: 类 App\CustomerRepository 的对象无法转换为 int
你能帮帮我吗?
【问题讨论】:
-
Nitpick:这不是单元测试,而是集成测试,因为您不会模拟数据库的东西。
标签: php laravel unit-testing phpunit