Speed ​​up RSpec & Factory tests for girls?

I am currently using FactoryGirl and Rspec to test my models, which is great but incredibly slow. The hundreds of tests I have for each model take about 30 seconds for each model.

The main problem is that when I create an object for testing, I use the method FactoryGirl.create()

. This hits the DB and is definitely slower than using build

or build_stubbed

. But if I just use the assembly, then I will never know if I ran into an error with the right database (for example, try to write a null value to a column that I indicated as non-empty)?

Is there a way to get the best out of the world? Or should I test the DB integration part explicitly somewhere outside of the model / unit tests?

+3


source to share


3 answers


Don't know if this applies in your case, but have you considered setting up spec_helper.rb to make your package run faster?



I have documented the evolution of my spec_helper.rb file at fooobar.com/questions/778580 / ... (see in particular "Edit 4") and links to other SO answers and blogs listed there helped me cut the time significantly work package.

+2


source


I use FactoryGirl.build or just .new

to instantiate in model specs and then only store them if some behavior needs to be tested in the test that requires a persistent instance.



This can be problematic when using associations or unions in which a string identifier must be present. It's kind of a trade-off - quick tests versus tests that are easy to write.

0


source


you have to use assembly most of the time, I want to be sure that some value will not be stored as null, for some spec it just doesn't make sense to always create objects on db

if you check that the factory creates a valid object once, you can trust the factory to always create valid objects.

also always use presence checks on fields that cannot be null / nil, if your field is not nil then you can be sure the db will not be null

0


source







All Articles