いくつかの単体テストを作成する必要があるアプリケーションに取り組んでいます。単体テストでデコレーター '@login_required' をモックするにはどうすればよいですか? これは app.py に @login_required 関数を持つ関数です
@app.route('/settings', methods=['GET', 'POST'])
@login_required
def settings():
global application_inst
if request.method == 'POST':
print("Setting changed")
return render_template('settings.html', user=session['user'], application=application_inst)
これがtest_app.pyのユニットテストケースです
class MyTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app(db)
self.app.config['TESTING'] = True
self.app.config['LOGIN_DISABLED'] = True
self.app.config['WTF_CSRF_ENABLED'] = False
self.app.config['DEBUG'] = True
self.client = self.app.test_client(self)
def test_settings_passed(self):
with self.client:
response = self.client.get('/settings', follow_redirects=True)
self.assertEqual(response.status_code, 200)
テストに合格する方法がなかったので。status_code = 404 を想定しているため、200 です。インターネットで入手できるものはすべて試しましたが、問題は解決しませんでした。したがって、デコレータを嘲笑してみたかったのです。どうすればいいですか?長い間この問題に悩まされているので、助けてください。