Why do we use factories for testing?

By: Johnathon Wright on: September 23, 2009

A client recently asked me why we used factories in our tests. Here's my answer:

Originally, people would create a new instance of every model in every test. So, if you were testing that we properly parse dates and know which ones are valid and which aren't, and you know that Person requires a name, you might write:

Person.new(:name => 'fiona', :dob => '3/5/12').should be_valid

and that was cool

then eventually:

validatespresenceof :emailvalidatespresenceof :favorite_color

then

Person.new(:name => 'fiona', :email => 'fiona@gmail.com', :dob => '3/5/12', :favoritecolor => 'red').should bevalid

and

Person.new(:name => 'fiona', :email => 'fiona@gmail.com', :dob => '3/12', :favoritecolor => 'red').shouldnot be_valid

just to make sure we were properly parsing dates and ensuring we got a complete one.

and so that was moderately painful, but then...

we split name into firstname and lastname

and then we have to change 75 tests

so then someone said, "This is really painful."

"Let's create a default Person, and just modify it when we want to test things."

and the solution followed the "GoF pattern called Factory":http://en.wikipedia.org/wiki/Abstractfactorypattern so that's what they called it.





Comments:

Just checking that you are human. What would be the result of this code?

a = 3*(4/2); b = 1; a+b

Maryellen said: You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I will try to get the hang of it! casino en ligne Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but instead of that, this is great blog. An excellent read. I'll certainly be back. casino en ligne Thanks for ones marvelous posting! I genuinely enjoyed reading it, you can be a great author. I will always bookmark your blog and will eventually come back very soon. I want to encourage one to continue your great work, have a nice weekend! casino en ligne I'm curious to find out what blog system you are using? I'm having some small security problems with my latest site and I would like to find something more safe. Do you have any recommendations? casino en ligne francais Incredible points. Outstanding arguments. Keep up the great effort. casino en ligne Wow that was unusual. I just wrote an extremely long comment but after I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that over again. Regardless, just wanted to say wonderful blog! casino en ligne This is really interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your excellent post. Also, I have shared your site in my social networks! casino en ligne I have been exploring for a little bit for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I ultimately stumbled upon this site. Reading this info So i am satisfied to show that I've an incredibly just right uncanny feeling I came upon exactly what I needed. I so much for sure will make sure to do not put out of your mind this website and give it a look regularly. meilleur casino en ligne Hello! Would you mind if I share your blog with my zynga group? There's a lot of folks that I think would really enjoy your content. Please let me know. Many thanks casino en ligne You actually make it seem so easy with your presentation but I find this topic to be really something that I think I would never understand. It seems too complex and very broad for me. I'm looking forward for your next post, I'll try to get the hang of it! casino en ligne
Back