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?

More than 1 year has passed since last update.

テストコード

0
Posted at

本番コードでは

# ここでdbを読み込み、SQLAlchemy はこのオブジェクトの変更を 自動的に追跡
user = User.query.filter_by(verification_token=token).first() 

"""
dbに情報を追加
"""

db.session.commit() # 最後に変更を確定させる

のように実際に存在するdbを操作するため、ここで呼び出してそこからは自動で追跡するから最後にdb.session.commit()を使う


テストコードでは

# helper.py
from models import User

def create_test_user(): # テストユーザーを作成
    user = User()
    return user

# test_route.py
from helper import create_test_user

user = create_test_user() # テストユーザーモデルを呼び出す

db.session.add(user) #ここでセッションに追加、dbの追跡開始
db.session.commit()

のように実際のdbに触れないようにテスト専用のdbを利用するからdb.session.add(user)この操作が必要

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?