Here is an article on it, in case you are not familiar: https://martinfowler.com/bliki/ObjectMother.html
I am wanting to use this to create test data. However, there are some disagreements on the team about how exactly to use this pattern.
The contention is about something I am going to refer to as reference data.
Reference data are tables in databases that are references by other tables but are immutable.
For example, lets take a State table which has a column for an State name, like “Florida”, and then another column for an abbreviation like “FL”. Maybe there are a bunch of other columns too.
Now I have another table, “Bill”. This represents some kind of bill, with line items and a bunch of other columns. One of these is a State foreign key, just so you know what state the Bill is from.
As a tester, I may want to make sure that the total, on the bill is the sum of the Bill’s line items. I do not care about the State column at all. Its incidental.
Now using the Object Mother pattern, I want to create a system that will fabricate a bill, which will then be persisted to the database, then the tests will run, and then Ill remove the bill.
Now to get to my question.
There are two schools of thought on the team.
Method 1: Create the bill, but all reference data, is gotten from the database. It is not fabricated because it is immutable and there is a lot of it (lots of tables, and some of them are quite large). The mother pattern will be used, but when a bill is created, the State will be an object with a real ID and data from the database instead of being fabricated.
The advantage is that you do not need to create / destroy and keep up to date reference data. If some table gets new items, then you are good because you just pull from the real tables. Same with deletions and updates.
The disadvantage that I can see with this method, is that you will need a real database (which our current testing needs right now). Also reference data could become corrupt (someone drops a table, deletes records, or just does something stupid).
Method 2: All data is fabricated, even reference data. This the most “pure” approach.
There are some big drawbacks to this pattern. You need to code all and create all reference data. You will need also keep this code up to date with important changes. You will need to populate all this data with each test case, and then clean it up. You also can’t run this against a database with any actual data in it. You could easily run into issues such as duplicate keys. For example, lets say that the State name in our
State table had to be unique. If there was a State of “Florida”, if you tried to create this reference item, it would blow up in your face.
The really big advantage of this method is that you can have an in-memory database to run all your tests. It would allow you to easily run these tests regardless of environment. You also have no fear of your test data being corrupted.
Anyway, I would like to know what people think is the right way. Also if you can provide links to support your point of view, it would greatly be appreciated. We have tried googling this, but so far we have not found any definitive answers to this question.