大家好,我是单元测试的新手,对此我一无所知
这是我正在测试的存储库方法=>
public function store(array $input){
$input['billing_contact'] = (isset($input['billing_contact'])) ? 1 : 0;
\DB::beginTransaction();
try {
$new_contact = $this->model->create($input);
//EVENT CREATE ACCOUNT (USER)
event(new EventCreateUserWithContact($new_contact));
\DB::commit();
}
catch(\Exception $e){
\DB::rollback();
return false;
}
return $new_contact;
}
这是我要进行的测试=>
class ContactTest extends TestCase
{
use WithFaker;
protected $contact;/**/
protected $container;/**/
public function setup()
{
parent::setup();
$this->container = new \Illuminate\Container\Container();
$this->contact = new Contact();
DB::beginTransaction();
}
public function tearDown()
{
DB::rollback();
parent::tearDown();
}
/**
* A basic test example.
*
* @return void
*/
public function testContactRepo()
{
// Event::();
$publisher = factory(\App\Models\Publisher::class)->create();
$contact = factory(\App\Models\Contact::class)->create();
//
$data = [
'first_name' => $this->faker->word,
'last_name'=> $this->faker->word,
'email' => $this->faker->unique()->safeEmail,
'phone'=> 112321321,
'job'=> $this->faker->word,
'billing_contact'=> $this->faker->word,
'approve_delete_at'=> $this->faker->date('Y-m-d', '1461067200'),
'publisher_id'=> $publisher->id,
];
$rep = new ContactRepositoryEloquent($this->container);
$contact = $rep->store($data);
dd($contact);
}
}
我不知道如何获取新的联系人对象,因为在运行测试时出现异常错误(我为假) 我是否需要创建一个假事件才能使其正常工作?
答案 0 :(得分:0)
我使用=>
找到解决方案\ Illuminate \ Support \ Facades \ Event :: fake();
它有助于通过事件并继续测试!