0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Supabaseを使ったアプリのテストで詰まった話と、モックで解決した方法

0
Posted at

はじめに

学習記録アプリのテストを書こうとしたら、Supabase(DB)と通信する部分が邪魔でテストがうまく動かなかった。
調べたら「モック」という仕組みで解決できたので整理する。

問題

自分のアプリでは supabaseFunction.js がSupabaseと通信してデータを取得・追加・削除している。
テストでこのコードをそのまま使うと:

  • ネット環境がないと動かない
  • テストのたびにDBのデータが変わる
  • 通信する分、遅い

テストしたいのはアプリの画面やロジックなのに、DB通信のせいで安定しない。

解決方法

モック(mock)を使う。モックとは、本物の代わりに使う偽物のこと。

本物と同じインターフェースを持つ偽物に差し替えることで、通信せずに固定のデータを返せるようになる。
結果がいつも同じになるので、テストが安定する。

終わりに

今回の経験から、テストは「正しく動くか」だけでなく「安定して再現できるか」が大切だと学んだ。
モックはそのための強力な手段なので、今後の開発でも活用していきたい。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?