Factories/Fixtures vs simple Model.create(...)?
factory-bot, ruby-on-rails
Solution
Having Factories and following the Test Data Building pattern will make you do a little bit of work up front, but will really save you time and work in the future.
Let's say you have a Car model, and that Car has an Owner, and that Owner needs an Address. Also each of those have other fields. If you want to follow the approach of using the Models directly, you will have to create those objects (and the correspondent relations) in each step definition that requires them. With Factory? You will define it just once.
Once you have defined the Factory in a single place with its correspondent structure, all you will have to do is ask for a Car and Factory will take car of all the dependencies with other models. Isn't that cool? At the end you want to focus on testing at this point.
What it's also really cool is that you can overwrite particular attributes, so you might have for example something like this, if you want to overwrite the attribute speed:
Given /^I have a car running^/
Factory :car, speed => 100
end
From my humble point of view, I love Factory Girl because it makes my test code easy to mantain and really easy to read.
Problem
What's the purpose of factories/fixtures (I know factories act like fixtures, but a bit clearer) when you can simply use ActiveRecord in your test to create the database entry? i.e. News.create(…) I just don't see any advantage of using Factory Girl instead of simply create a new let say a new User using ActiveRecord methods.. Thanks