开发者

Factory Girl not destroying objects properly

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开发者_JS百科 the articles association not being reduced by one?

Thanks!


You need to reload the articles collection as Rails' database caching is stale:

base_article = Factory(:base_article, :articles => [Factory(:article)])
base_article.articles.size # => 1

base_article.articles.first.destroy
base_article.articles.size # => 1
base_article.articles.reload.size # => 0

base_article.destroyed?.should == true 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜