Factory Girl - What is the Purpose?

What is the purpose of Factory Girl in rspec to check when I could use before(:each) blocks? It seems that the only difference between Factory Girl and before(:each) is that Factory is preparing to create an object outside of the test. Is it correct?

+57
ruby factory-bot
Mar 03 '11 at 17:04
source share
3 answers

Gems, such as Factory Girl and Sham , allow you to create patterns for valid and repeated - useful objects. They were created in response to instruments in which fixed records were to be loaded into a database. They allow you to customize more when you create objects, and they aim to have a valid object to work with. They can be used anywhere in your tests and in your before and after test hooks.

before (: each), before (: all), after (: each) and after (: all) are all aimed at giving you room for customization and breaks to be used among the test group. For example, if you are going to create a new real user for each individual test, then you will want to do this in your hook (: each). If you are going to clear some files from the file system, you want to do this at the beginning. If your tests create a tmp file and you want to delete it after the test, you will do it in your own after (: each) or after (: all).

The differences between the two concepts are that factories are not aimed at creating hooks around your tests, they are aimed at creating real objects and Ruby records, so you can keep your object's flexibility dry. Before and after the interception, they are aimed at tuning and disassembling tasks, which are shared in a group of examples so that you can save the installation and break code DRY.

+59
Mar 03 '11 at 17:22
source share

FactoryGirl replaces appliances in tests. Thus, you will not need to update fixtures when changing the data model. Light fixtures can also be cumbersome as you add more edge cases.

FactoryGirl generates data on the fly, and adding and deleting fields is much easier. In addition, you can use it in customizing how to use the lights.

Is this clearer?

+17
Mar 03 '11 at 17:18
source share

also instrument definitions are global throughout the application. Factories can be local - therefore, the data specific to an isolated test case is in the settings for this specific context, and not in one global fixture file.

The plot is very well covered in this book if you want to know more about reading

+6
Mar 03 '11 at 17:23
source share



All Articles