私には疑問が1つありますテストメソッドの名前付け方法最後に大きなプロジェクトに単体テストを行い、経験に基づいて名前テストメソッドが悪いと思う私のコードについての例をここに示します。 テストメソッドの名前を正しく指定するにはどうすればいいですか
public function notificationApproved(Request $request, User $user) {
$user = $user->getId();
$request = $request->getRequest();
$this->notification = $this->em->getRepository('AppBundle:Notification')->find(Notification::APPROVED);
$this->notificationCategory = NotificationCategory::APPROVED;
$this->notificationStatus = $this->em->getRepository('AppBundle:NotificationStatus')->find(1);
$this->reason = $reason;
//notification approvers project
foreach ($projectHasUserUnits as $keyData => $valueData) {
$projectUserUnitResponsability = $valueData->getProjectUserUnitResponsability()->last();
$responsability = $projectUserUnitResponsability->getResponsabilityProject();
if (is_null($projectUserUnitResponsability->getEndAt()) && ($responsability->getId() == Responsability::TYPE_RESPONSIBLE || $responsability->getId() == Responsability::TYPE_ACCOUNT_MANAGER || $responsability->getId() == Responsability::TYPE_PROJECT_MANAGER)) {
$user = $valueData->getUserHasUnit()->getUser();
if(!in_array($user,$this->users)){
$this->users[] = $user;
$description_label = 'notification_description_project_50';
$short_description = 'notification_content_project_50';
$notification = $this->generateNotification($Project, $user, $description_label,$short_description);
if ($notification) {
$this->notificationTrigger
->sendProjectNotification($user, $notification, $Project
, $notification->getDescription(), date('l m-d-y H:i a'));
}
}
}
}
}
それで大きな方法、私は知っているが、それ自体はすべてで来たときにだけ、この場合は、条件文を参照してください、そして、このようなメソッドのテストに名前を付ける方法を考えて、そのロジックのために心配しないでください条件:...多分:
test_notificationApprovedWhenAllConditionsAreTrue
test_notificationApprovedWhenProjectHasUserUnitsIsBiggerThanZeroAndProjectUserUnitResponsabilityGetEndAtIsNotNullAndResponsabilityGetIdIsEqualToResponsabilityTYPERESPONSABILITYorResponsabilityGetIdIsEqualToResponsabilityTYPE_PROJECT_MANAGERanduserInArrayIsTrueAndNotificationIsTrue...
あなたはそれを読んでいると想像してください!
しかしそれだけで1つの条件が簡単です来るときに上記、もちろん!このような!
test_notificationApprovedWhenProjectHasUserUnitsIsBiggerThanZeroAndProjectUserUnitResponsabilityGetEndAtIsNotNullAndResponsabilityGetIdIsEqualToResponsabilityTYPERESPONSABILITYorResponsabilityGetIdIsEqualToResponsabilityTYPE_PROJECT_MANAGER
私はテスト内部のコメントを試してみたが、テストが失敗した場合、アイデアが希望その名前のために指導してください(エラーを迅速に修正するため)
あなたは考えていますか?
コードの品質の一部を少し明確にするために私の答えを少し編集しました。 –
ロジックの複雑さがわからないときは別のロジックを試してみるのは難しい – jjoselon
リファクタリングは別のトピックですが、このネーミングスキームはまだ使用できます。 –