简体   繁体   中英

Testing attribute methods with RSpec Rails

Recently I have migrated one of my models from a self made (parent_id based) tree structure to the ancestry gem and I have a failing controller spec for the new action. The line in question is:

assigns(:category).should be_a_new(Category).with(parent_id: root_category.id)

However it complains that there's no attribute parent_id and indeed it isn't as parent_id is now a method. How should I modify the above line to check for both a new category and that category.parent_id == root_category.id ?

Instead of 'parent_id' you can use the column name ancestry uses to store the parent's id which is 'ancestry' by default. You can change the name by setting it as option to the has_ancestry class method, eg has_ancestry(ancestry_column: :parent_id) .

Your modified test code would look as follows:

assigns(:category).should be_a_new(Category).with(ancestry: root_category.id.to_s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM