让 RSpec 销毁记录而不是删除它们

发布于 2024-12-16 21:12:30 字数 456 浏览 0 评论 0原文

在我的应用程序中,我有一个模型,它具有 after_create 和 after_destroy 方法,它们创建一个目录,然后分别在文件系统上销毁它。

我注意到在我的 RSpec 请求规范中创建了模型记录(因此创建了目录),但我假设记录只是在每个规范之后删除而不是销毁,因此目录会挂起而不是被删除。或者 RSpec 只是回滚整个规范的数据库事务?

我的问题是,如何使 RSpec 安全地销毁这些记录,以便文件系统保持同步?

我已经尝试过:

after(:each) do
  Entity.destroy_all
end

这有效,但我不确定这是最好的方法。

也许我可以在模型本身中做得更好,这样如果我检测到事务回滚,我也可以删除该目录?我从未听说过此功能,尽管可能是在 beginrescuesure 块中。

有什么建议吗?

In my app I have a model that has after_create and after_destroy methods that create a directory and then destroy it respectively on the filesystem.

I've noticed in my RSpec request specs that the model records get created find (thus the directories get created), but I'm assuming the records are just deleted after each spec rather than destroyed, thus the directories hang around instead of being deleted. Either that or perhaps RSpec just rolls back the database transaction around the whole spec?

My question is, how can I make RSpec safely destroy these records so that the filesystem stays in sync?

I've tried:

after(:each) do
  Entity.destroy_all
end

and that works, but I'm not sure it's the best way.

Perhaps I could be doing it better in my model itself so that if I detect a transaction rollback I can delete the directory then too? I've never heard of this functionality though bar perhaps in a begin rescue ensure block.

Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜雨飘雪 2024-12-23 21:12:30

事实证明,您可以关闭某些规范的事务,例如需要完全添加记录并运行某些回调后的事务。我还通过 after_create 修改为 after_commit(:on => :create) (对语法不是 100% 确定,只是从内存中获取)来添加和删除文件系统元素,因为这是确保记录存在于在进行文件系统更改之前更改数据库。

Turns out that you can turn off transactions for certain spec's like ones that need to completely add the record and run certain after callbacks. I also modified by after_create to an after_commit(:on => :create) (not 100% sure on the syntax, just taking that from memory) to add and remove filesystem elements because that's the safest way to ensure that the record exists in the database before making the filesystem changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文