Answer the question
In order to leave comments, you need to log in
How to write a unit test correctly?
Hello! I understand tests. It turns out that I work with Laravel framework and AngularJS. Most of my methods return JSON objects.
Well, an example of a standard method:
/**
* Update company.
*
* @param int $id Company id
* @param Request $request Company data
* @throws ModelNotFoundException If company is not found
* @return Response
*/
public function update(int $id, Request $request): Response
{
try{
$company = Company::findOrFail($id);
$rules = array(
'email' => 'required|email|unique:companies,email,' . $request->id. ',id',
'name' => 'required',
'address' => 'required',
);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
$messages = $validator->messages();
return response([
'error' => $messages
], 400);
} else {
$company->name = $request->name;
$company->email = $request->email;
$company->address = $request->address;
$company->save();
return response([
'success' => true, 'data' => $company
], 200);
}
}catch (ModelNotFoundException $e){
return response([
'error' => true
], 400);
}
}
Answer the question
In order to leave comments, you need to log in
1) it's not a unit test, it's an integration test. It will check the "part of the system assembly", in your case the apish. If you were testing through the UI (that is, through angular) - this would be called an end-to-end test (from buttons to the database they say).
2)
On the Internet, I see simple examples of tests that check for true false
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question