Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test/routing #8

Merged
merged 7 commits into from Jan 11, 2019
Merged

test/routing #8

merged 7 commits into from Jan 11, 2019

Conversation

ha-matsumu
Copy link
Owner

@ha-matsumu ha-matsumu commented Jan 9, 2019

router.jsのテストを作成しました。レビューよろしくお願いいたします。

package.json Outdated
@@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "nodemon src/index.js",
"test": "mocha ./test/src/models"
"test": "mocha ./test/src/routes"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こうしてしまうと、modelsのテストがnpm test で実行できなくなります。
既存のテストもnpm test実行できるように修正お願いしますmm

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testディレクトリにmocha.optsを作成して、npm testでmodelsのテストも行えるようにしました。


describe("GET /", () => {
it("top.ejsの表示", (done) => {
request(app)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GET /GET /quiz で以下のコードが重複しているので、共通部分は1つの関数に抜き出すようにリファクタお願いします。

request(app)
      .get("/")
      .expect(200, done);

その際に、getの引数に渡すURLやコールバック関数はリファクタ時に作成する関数の引数経由で渡すようにしてあげると良いでしょう。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

また、app.use("/api", quizRouter); に関するテストが見当たらないですが、apiのテストはまた別のプルリクで作成するという認識でよろしいでしょうか?

https://github.com/ha-matsumu/trivial-quiz-express/blob/test/routing/src/index.js#L12

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apiのテストも作成しました。

@tsuyopon-xyz
Copy link

コメントしたのでご確認お願いしますmm

@ha-matsumu
Copy link
Owner Author

コードの修正とapiのテストを作成しました。
レビューよろしくお願いいたします。


describe("GET /", () => {
it("top.ejsの表示", (done) => {
runRouteTest("/", done);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かいですがインデントのスペースが1つになっているので、他と統一して2つにお願いしますmm

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

インデント修正しました。


describe("GET /quiz", () => {
it("quiz.ejsの表示", (done) => {
runRouteTest("/quiz", done);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test/src/routes/quizRouter_test.js がreturnを使ったPromiseスタイルでの非同期処理のテスト実装に対して、こちらのテストは done を使ったコールバックで非同期処理のテストを終了するスタイルにしている理由は何かありますでしょうか?
(テストの実装自体は問題ないのでこのままでもOKです^^ 統一されていない書き方が気になったので質問しました。)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特に理由はないので、router_test.jsをPromiseスタイルに変更しました。

@tsuyopon-xyz
Copy link

👍

実装自体問題ありません^^
気になったところだけコメントしたのご確認お願いしますmm

@ha-matsumu
Copy link
Owner Author

ha-matsumu commented Jan 10, 2019

修正しました。
#8 (comment)
#8 (comment)
レビューよろしくお願いいたします。


describe("GET /", () => {
it("top.ejsの表示", () => {
runRouteTest("/");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise形式で実装するのであれば、itの中にreturnでPromiseオブジェクトをセットする必要があると思いますが、 returnなしで実装した場合runRouteTest("/");runRouteTest("/abc"); のように存在しないURLに変更した場合でもテストは失敗しますでしょうか?

ドキュメント抜粋
image

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コード

function runRouteTest(_url) {
  request(app)
    .get(_url)
    .expect(200);
}

describe("GET /", () => {
  it("top.ejsの表示", () => {
    runRouteTest("/abc");
  });
});

テスト実行結果
router_test

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上記のようになりました。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/abc のルーティングは容易していないのでテストは失敗するべきですが、テストが通っているのはおかしい挙動となります。

/abc にしたまま、以下のようにitの中にreturnを使ったらテストは落ちるようになるかなと思いますがいかがでしょうか?

function runRouteTest(_url) {
  request(app)
    .get(_url)
    .expect(200);
}

describe("GET /", () => {
  it("top.ejsの表示", () => {
    return runRouteTest("/abc");
  });
});

テストでは、正しいURLを入力したときに意図した挙動をする正常系と、意図しない値をセットしたときにエラーや例外を発生する異常系の両方を合わせて容易しておくと、よりテストの安全性が保証されます。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下のようにコードを変更するとテストが落ちました。

function runRouteTest(_url) {
  return request(app)
    .get(_url)
    .expect(200);
}

describe("GET /", () => {
  it("top.ejsの表示", () => {
    return runRouteTest("/abc");
  });
});

describe("GET /quiz", () => {
  it("quiz.ejsの表示", () => {
    return runRouteTest("/quiz");
  });
});

router_test2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下の部分が意図したとおり200ではテストが失敗するようになっていますね^^

describe("GET /", () => {
  it("top.ejsの表示", () => {
    return runRouteTest("/abc");
  });
});

以下3つのパターンのテストを用意して全てのテストが通るようにしてください。(全てpassingになるようにする。(緑の✔だけ表示されるようにする))

- `GET /` → 成功 (ステータスコード200)
- `GET /quiz` → 成功 (ステータスコード200)
- `GET /abc` → 失敗 (ステータスコード404)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3つのパターンのテスト通るように修正しました。

@tsuyopon-xyz
Copy link

1点コメントしたのでご確認お願いしますmm

@ha-matsumu
Copy link
Owner Author

コード修正しました。レビューよろしくお願いいたします。

@tsuyopon-xyz
Copy link

👍

OKです^^
マージしても構いません^^
DBの学習に入って頂けたあらと思います^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants