Upgrade to Pro — share decks privately, control downloads, hide ads and more …

自動テストの2歩目

chiroruxx
February 03, 2020

 自動テストの2歩目

2020/02/03 タピオカLTで発表する資料です。

chiroruxx

February 03, 2020
Tweet

More Decks by chiroruxx

Other Decks in Technology

Transcript

  1. ࣗಈςετͷา໨
    λϐΦΧ-5
    લా࿨ਓ

    View Slide

  2. ࣗݾ঺հ
    w લా࿨ਓ
    w !DIJSPSVYYYY
    w 3099

    View Slide

  3. ࠷ॳʹ
    w ࣗಈςετͬͯ೉͘͠ͳ͍Ͱ͔͢ʁ
    w BTTFSU&RVBMT
    Έ͍ͨͳαϯϓϧ͔͠ͳ͍
    w ͔ͦ͜ΒϓϩμΫγϣϯͷίʔυॻ͘ͷແཧ͘ͳ͍ʁ

    View Slide

  4. ࠷ॳʹ
    w ๻΋͍ͬͺ͍ඍົͳςετίʔυॻ͍ͨ
    w ඍົͳίʔυ΋͍ͬͺ͍ݟ͖ͯͨ
    w ͋ΕɺΈΜͳಉ͡Α͏ʹϋϚ͍ͬͯΔͷͰ͸ʁ
    w ؕΓ͕ͪͳύλʔϯͱɺͲ͏͢Ε͹͍͍͔Λ঺հ͢ΔΑ
    w ݸਓతͳࣄ৘ʹΑΓ1)1ͩΑ

    View Slide

  5. ಉ͡ϩδοΫ
    Ξϯνύλʔϯ

    View Slide

  6. ಉ͡ϩδοΫ
    function sum(array $array)
    {
    $sum = 0;
    foreach ($array as $value) {
    $sum += $value;
    }
    return $sum;
    }
    public function testSum()
    {
    $input = [1, 2, 3];
    $expected = 0;
    foreach ($input as $value) {
    $expected += $value;
    }
    $this->assertEquals($expected, sum($input));
    }

    View Slide

  7. ಉ͡ϩδοΫ
    w ࣮૷ͱςετ͕ಉ͡ϩδοΫ
    w ͦ΋ͦ΋ςετʹͳͬͯͳ͍
    w ಉ͡ܭࢉͨ͠ΒͦΒಉ݁͡ՌʹͳΔΑͶ
    w খֶߍͷͱ͖ʹͯͨ͠ݕࢉ΋ҧ͏ܭࢉํ๏Ͱ΍ͬͯͨ͸ͣ
    ‎ ܾΊଧͪςετ͕༗ޮ

    View Slide

  8. ܾΊଧͪςετ
    public function testSum()
    {
    $this->assertEquals(6, sum([1, 2, 3]));
    $this->assertEquals(2, sum([2]));
    $this->assertEquals(0, sum([0, 0, 0]));
    $this->assertEquals(0, sum([]));
    }
    public function testSum()
    {
    $input = [1, 2, 3];
    $expected = 0;
    foreach ($input as $value) {
    $expected += $value;
    }
    $this->assertEquals($expected, sum($input));
    }

    View Slide

  9. σʔλϓϩόΠμ
    public function testSum(array $input, bool $expected)
    {
    $this->assertEquals($expected, sum($input));
    }
    public function dataProvider(): array
    {
    return [
    ['input' => [1, 2, 3], 'expected' => 6],
    ['input' => [2], 'expected' => 2],
    ['input' => [0, 0, 0], 'expected' => 0],
    ['input' => [], 'expected' => 0],
    ];
    }

    View Slide

  10. σʔλϓϩόΠμ
    ͷ࢖͍·Θ͠
    Ξϯνύλʔϯ

    View Slide

  11. σʔλϓϩόΠμͷ࢖͍·Θ͠
    public function testGetQuestionsWithoutDate(array $input, array $expected)
    {
    $expected['created_at'] = null;
    $expected['updated_at'] = null;
    $expected['deleted_at'] = null;
    $service = new QuestionService();
    $this->assertEquals($expected, $service->getQuestionsWithoutDate($input));
    }

    View Slide

  12. σʔλϓϩόΠμͷ࢖͍·Θ͠
    w ଞͷςετͰ࢖༻͍ͯ͠ΔσʔλϓϩόΠμΛແཧʹྲྀ༻
    w ड͚औͬͨ஋Λߋ৽ͨ͠ΓɺҰ෦Λ࢖Θͳ͔ͬͨΓ
    w ΋͸΍σόοΨ͕ͳ͍ͱಡΊͳ͍
    w ίʔυΛ௥͏࣌ʹσʔλϓϩόΠμ͔ΒԿΛ౉͞Ε͔ͨΛهԱ͠
    ͯɺͦͷهԱͷ஋Λߋ৽͠ͳ͍ͱ͍͚ͳ͍
    ‎ ςετσʔλϓϩόΠμ͕͓͢͢Ί

    View Slide

  13. ςετσʔλϓϩόΠμ
    public function getQuestionDataProvider(): array
    {
    return [
    'input' => [
    'title' => 'question title',
    ],
    'expected' => [
    'title' => 'question title',
    'author' => ‘default author',
    'created_at' => '2019-02-03 00:00:00',
    'updated_at' => '2019-02-03 00:00:00',
    'deleted_at' => null,
    ],
    ];
    }
    public function getQuestionWithoutDateDataProvider(): array
    {
    return [
    'input' => [
    'title' => 'question title',
    ],
    'expected' => [
    'title' => 'question title',
    'author' => ‘default author',
    ],
    ];
    }

    View Slide

  14. ςετσʔλϓϩόΠμ
    public function testGetQuestions(array $input, array $expected)
    {
    $service = new QuestionService();
    $this->assertEquals($expected, $service->getQuestions($input));
    }
    public function testGetQuestionsWithoutDate(array $input, array $expected)
    {
    $service = new QuestionService();
    $this->assertEquals($expected, $service->getQuestionsWithoutDate($input));
    }

    View Slide

  15. ςετσʔλϓϩόΠμ
    public function getQuestionDataProvider(): array
    {
    return [
    'input' => [
    'title' => 'question title',
    ],
    'expected' => $this->defaultQuestionAttributes(),
    ];
    }
    public function defaultQuestionAttributes(array $overrides = [])
    {
    $default = [
    'title' => 'question title',
    'author' => 'default author',
    'created_at' => '2019-02-03 00:00:00',
    'updated_at' => '2019-02-03 00:00:00',
    'deleted_at' => null,
    ];
    return array_merge($default, $overrides);
    }

    View Slide

  16. ϥϯμϜ஋ͷݕূ
    Ξϯνύλʔϯ

    View Slide

  17. ϥϯμϜ஋ͷݕূ
    public function testDelete()
    {
    $question1 = factory(Question::class)->create();
    $question2 = factory(Question::class)->create();
    $service = new QuestionService();
    $service->delete($question1);
    $this->assertDatabaseMissing(['title' => $question1->title]);
    $this->assertDatabaseHas(['title' => $question2->title]);
    }

    View Slide

  18. ϥϯμϜ஋ͷݕূ
    w ϥϯμϜʹੜ੒͞Εͨ஋Ͱൺֱɾ֬ೝ͢Δ
    w ϥϯμϜ஋ʹΑͬͯ͸མͪΔ৔߹͕͋Δ
    w ςετΨνϟͷൃੜ
    w ͦ΋ͦ΋ͳͥϥϯμϜ஋Λ࢖͏ͷ͔Λߟ͑Α͏
    ‎ ϥϯμϜ஋Λൺֱɾ֬ೝ͠ͳ͍

    View Slide

  19. ϥϯμϜ஋ͷݕূ
    public function testDelete()
    {
    $question1 = factory(Question::class)->create(['title' => 'ফ͑Δ']);
    $question2 = factory(Question::class)->create(['title' => '࢒Δ']);
    $service = new QuestionService();
    $service->delete($question1);
    $this->assertDatabaseMissing(['title' => 'ফ͑Δ']);
    $this->assertDatabaseHas(['title' => '࢒Δ']);
    }

    View Slide

  20. ·ͱΊ
    w ܾΊଧͪςετΛ͠Α͏
    w ςετσʔλϓϩόΠμ
    w ϥϯμϜ஋Ͱൺֱɾ֬ೝΛ͠ͳ͍

    View Slide