Я делаю юнит-тесты, используя PHP (7.3), Laravel (7.6) и издевательство (1.3). Мне нужно сделать 2 издевательства на 1 класс (красноречивая модель). У кого-нибудь есть идея?
Вот рабочий код.
public function update(Request $request, $id)
{
$request->validate([
'first_name'=>'required',
'last_name'=>'required',
'email'=>'required'
]);
$contact = Contact::find($id);
$contact->first_name = $request->get('first_name');
$contact->last_name = $request->get('last_name');
$contact->email = $request->get('email');
$contact->job_title = $request->get('job_title');
$contact->city = $request->get('city');
$contact->country = $request->get('country');
$contact->save();
return redirect('/contacts')->with('success', 'Contact updated!');
}
Вот тестовый код. Что касается "Contact :: find ()", я успешно сделал макет с помощью (A) ниже. Но строка (B) вызывает ошибку, хотя я попытался сделать макет для (C) ниже.
public function testUpdateNormal()
{
//(A)
$this->mock = M::mock('alias:App\Contact')->makePartial();
//(B)
//$this->mockInstance = Mockery::mock('overload:App\Contact');
$valuesToBeSent = array(
'first_name'=>self::FIRST_NAME,
'last_name'=>self::LAST_NAME,
'email'=>self::EMAIL,
'city'=>self::CITY,
'country'=>self::COUNTRY,
'job_title'=>self::JOB_TITLE);
//Posted values should be passed to this method as an argument.
$this->mock->shouldReceive('find')->once()->with(self::ID)->andReturnUsing(function(){
echo PHP_EOL . 'The mock was called in testUpdateNormal().' . PHP_EOL;
//(C) I want to replace this instance with a mock.
$newContact = new Contact();
return $newContact;
});
$putTo = '/contacts/' . self::ID;
$response = $this->put($putTo, $valuesToBeSent);
$response->assertRedirect('/contacts');
$this->assertEquals('Contact updated!', session('success'));
//Check all values output in the page.
$response->assertSee(self::FIRST_NAME);
$response->assertSee(self::LAST_NAME);
$response->assertSee(self::EMAIL);
$response->assertSee(self::CITY);
$response->assertSee(self::COUNTRY);
$response->assertSee(self::JOB_TITLE);
//Check header items in the page.
$response->assertSee('First Name:');
$response->assertSee('Last Name:');
$response->assertSee('Email:');
$response->assertSee('City:');
$response->assertSee('Country:');
$response->assertSee('Job Title:');
}