工厂女孩没有正确破坏物体
我正在创建一个具有多个 has_many 关联的对象。关于构建对象的一切都工作正常,但是当我尝试测试删除子级或父级之一时,它不会反映在测试中。
例如:
base_article = Factory(:base_article, :articles => [Factory(:article)])
p base_article.articles.size
base_article.articles.first.destroy
p base_article.articles.size
base_article.destroyed?.should == true
这打印出:
1
1
我正在测试当没有更多子项时删除基础的文章上 destroy 后的回调。为什么文章关联的大小不减一?
谢谢!
I am creating an object with several has_many associations. Everything with regards tob building the object works fine, but when I try to test the deletion of one of the children or parent, it does not reflect in the test.
For example:
base_article = Factory(:base_article, :articles => [Factory(:article)])
p base_article.articles.size
base_article.articles.first.destroy
p base_article.articles.size
base_article.destroyed?.should == true
This prints out:
1
1
I am testing the callback after destroy on the article that deletes the base when there are no more children. Why is the size of the articles association not being reduced by one?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要重新加载文章集合,因为 Rails 的数据库缓存已过时:
You need to reload the articles collection as Rails' database caching is stale:
您可以改用articles.count。 “大小”为您提供内存中集合的长度。 Count 执行查询并从数据库获取最新的总数。
You could used articles.count instead. 'Size' gives you the length of the in-memory collection. Count does a query and gets the latest total from the db.