python - 修补 Django FileSystemStorage 位置不生效 [pytest]
问题描述
我有一个简单的模型:
#models.py
from django.core.files.storage import FileSystemStorage
LOCATION = 'old_location'
class FileArchive(models.Model):
file = models.FileField(storage=FileSystemStorage(location=LOCATION))
我有一个视图,通过它我可以填充提到的模型并调用 .save()。我的目标是进行功能测试以尝试上传文件并断言该文件存在于提供的(修补的)位置上。
问题是无论我如何修补LOCATION
位于 mymodels.py
中的 ,测试仍然使用我模型中的硬编码位置。
以下是我尝试修补的方法LOCATION
。
我尝试修补 LOCATION 属性的一些方法:
from . import models
def test_uploaded_file_is_stored(monkeypatch):
monkeypatch.setattr(models, 'LOCATION', 'new_location')
...
另一种方式:
def test_uploaded_file_is_stored(mocker):
mocker.patch.object(modesl, 'LOCATION', 'new_location')
...
另一种方式:
from unittest.mock import patch
def test_uploaded_file_is_stored(mocker):
with patch('filestorage.models', 'LOCATION', 'new_location')
...
我猜我正在修补错误的地方。有任何想法吗?
解决方案
推荐阅读
- go - 当我使用 `golang.org/x/time` NewLimiter() 时,限制更高的爆发,Allow() 将返回真正的更高的爆发
- oracle - ORA-00907: 过程返回表缺少右括号
- python - 我想将原始 HTML 传递给 QWebEngineView 但出现错误
- sql - 是否可以从临时表中恢复?
- templates - 让 Task 调用具有已知特定返回类型的任意函数的正确方法是什么?
- javascript - 每次重新渲染父组件时如何重新渲染子组件?
- spring-boot - Spring Boot RestController 在没有 SpringBootApplication 的情况下如何工作?
- gradle - 如何持久启用 Gradle 配置缓存功能?
- raku - 内省捕获的更好方法
- java - Android Studio 为 AIDL 生成 JavaDoc