简体   繁体   中英

In Rails, how to speed up machinist tests?

I'm replacing test fixtures with Machinist. But using Machinist to set up test data is very slow, because whenever a test method is run some new data are made by Machinist and saved to database. Is there any way to cache the data in memory so that using Machinist isn't so slow?

Thanks, Bryan

1) Try SomeModel.make_unsaved if you don't actually need it to be saved.

2) Look for ways to create lighter versions of the objects. Maybe you can avoid creating the associated objects. For example: BlogComment.make(:gravatar => nil) # avoid creating the gravatar image Or if you usually need the lighter version, you could flip that around and have the regular BlogComment.make create the light object (no gravatar) whereas BlogComment.make(:with_gravatar) would do the extra work.

3) Avoid offsite hits to web services (Google maps, S3, etc.) if you're not specifically doing an integration test for that service. Use stubbing to prevent those methods on the object you're testing from actually connecting to the external service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM