23 Oct 2012
Using factory girl and making mass assignment params
versions: factory_girl_rails: 4.1.0, rails: 3.2.8, rspec: 2.11.0
Writing controllers examples are pretty tough. For example, when you have to write some update method examples,
- first we gotta save a data to the DB which we are going to update,
- and prepare params(which is based on the data which we saved in 1.) to send to the update method,
- and finnaly, execute the update method.
and when it goes with the nested object forms, it gets tougher.
I’ll show you how I did it, its kinda messed up, so if you have some better solution, please comment.
FactoryGirl Data(preparation)
first we need to get FactoryGirl ready, so we can get some test data and use those.
lets say we have an Article table and a ArticleBody table, the relations will be like bellow.
It’ll be basic nested objects using nested_attributes_for
and has_many
.
If you don’t understand those, there’s some good tutorial on rails cast which I mentioned here, so look at that.
the factory girl data will be like bellow
I basically read the GETTING_STARTED.md which was in the factory girl repo to get the factory.rb done.
getting shits done :p
now lets look back what we have to do which I mentioned earlier.
- first we gotta save a data to the DB which we are going to update,
- and prepare params(which is based on the data which we saved in 1.) to send to the update method,
- and finnaly, execute the update method.
1. first we gotta save a data to the DB which we are going to update,
this is basic, just like bellow.
2. and prepare params(which is based on the data which we saved in 1.) to send to the update method,
this is going to be the tricky part, I did it like this.
If you want to change the values inside the article_bodies, you can do it by writing like this.
3. and finnaly, execute the update method.
thats it!
feel free to comment :)
Takehiro Adachi at 13:19