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
test/routing #8
Conversation
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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のテストも行えるようにしました。
test/src/routes/router_test.js
Outdated
|
||
describe("GET /", () => { | ||
it("top.ejsの表示", (done) => { | ||
request(app) |
There was a problem hiding this comment.
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やコールバック関数はリファクタ時に作成する関数の引数経由で渡すようにしてあげると良いでしょう。
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apiのテストも作成しました。
コメントしたのでご確認お願いしますmm |
コードの修正とapiのテストを作成しました。 |
test/src/routes/router_test.js
Outdated
|
||
describe("GET /", () => { | ||
it("top.ejsの表示", (done) => { | ||
runRouteTest("/", done); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かいですがインデントのスペースが1つになっているので、他と統一して2つにお願いしますmm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
インデント修正しました。
test/src/routes/router_test.js
Outdated
|
||
describe("GET /quiz", () => { | ||
it("quiz.ejsの表示", (done) => { | ||
runRouteTest("/quiz", done); |
There was a problem hiding this comment.
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です^^ 統一されていない書き方が気になったので質問しました。)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
特に理由はないので、router_test.js
をPromiseスタイルに変更しました。
👍 実装自体問題ありません^^ |
修正しました。 |
test/src/routes/router_test.js
Outdated
|
||
describe("GET /", () => { | ||
it("top.ejsの表示", () => { | ||
runRouteTest("/"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上記のようになりました。
There was a problem hiding this comment.
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を入力したときに意図した挙動をする正常系と、意図しない値をセットしたときにエラーや例外を発生する異常系の両方を合わせて容易しておくと、よりテストの安全性が保証されます。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3つのパターンのテスト通るように修正しました。
1点コメントしたのでご確認お願いしますmm |
コード修正しました。レビューよろしくお願いいたします。 |
👍 OKです^^ |
router.jsのテストを作成しました。レビューよろしくお願いいたします。