我怎样才能获得更快的测试并仍然使用 Factory Girl?

发布于 2024-11-14 17:42:33 字数 380 浏览 1 评论 0原文

我真的很喜欢使用 Factory Girl 来设置我的测试。我可以在一行中建立关联链。例如:

Factory.create(:manuscript)

自动创建期刊、期刊所有者、手稿作者等。它使我能够保持我的设置块非常简单,这太棒了。

然而,这当然是有代价的。在后台创建多个对象意味着我的单元测试有时长达 0.8 秒。当您的应用程序很小时,这很好,但现在我已经进行了数百次测试,并且我的规格需要一分钟多的时间才能运行(不包括应用程序启动所需的时间)。开始感到痛苦。

我对任何太激烈的事情都不太感兴趣,比如嘲笑一切。至少当我的应用程序相对较小时,我想保留我的工厂女孩​​抽象。我只是想找到一种方法让他们工作得更快一点。

有什么建议吗?

I really like using Factory Girl to setup my tests. I can build chains of associations in a single line. For example:

Factory.create(:manuscript)

Automatically creates a journal, a journal owner, a manuscript author, etc. It allows me to to keep my setup blocks really simple, and that's fantastic.

However, there's a cost of course. Creating several objects in the background means my unit tests are sometimes as long as 0.8 seconds. That's fine when your app is small, but now I've got a few hundred tests and my specs take over a minute to run (not including the time it takes for the app to spin up). It's starting to feel painful.

I'm not especially interested in anything too drastic, like mocking everything. At least while my app is relatively small, I'd like to maintain my factory girl abstractions. I just want to figure out a way to make them work a bit faster.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-11-21 17:42:33

如果您正在测试对象行为并且不需要将它们实际保存到数据库中,则可以使用Factory.build(:model)。它基本上实例化对象及其关联,但不将其写入数据库。这将比创建和存储所有这些对象快得多。如果您仍想将部分或大部分对象写入数据库,您可以设置一个 SQlite 内存测试数据库。 这是一个示例

If you're testing object behavior and don't need to actually save them to a database, you can use Factory.build(:model). It basically instantiates the object and it's associations, but does not write it to the DB. This will be much faster than creating and storing all those objects. If you still want to write some or most objects to the DB, you can setup an SQlite memory test database. Here's an example

混浊又暗下来 2024-11-21 17:42:33

不确定这个问题有没有好的解决方案。正如 Beerlington 所建议的,您可以使用 Factory.build 而不是 Factory.create 来节省一些时间。但即使如此,也远不如测试普通的旧红宝石对象那么快。事实上,如果你非常关心速度,那么工厂女孩并不是一个好的选择。

也就是说,通过阅读整个套件并自由使用 ,我能够取得一些相当显着的速度改进rspec-set 宝石。这允许您为整组测试运行一次设置,并且仅运行一次。它与使用 before(:all) 类似,只不过它利用事务来重置每个规范之间的对象状态。

Not sure there's a good solution to this problem. As Beerlington suggested, you can save some time using Factory.build rather than Factory.create. But even that's not nearly as fast as testing a plain old ruby objects. The fact, it seems, is that factory girl is not a good choice if you're very concerned with speed.

That said, I was able to make some fairly significant speed improvements by reading through my entire suite and making liberal use of the rspec-set gem. This allows you to run your setup once -- and only once -- for the entire group of tests. It's similar to using before(:all) except that it takes advantage of transactions to reset the state of objects between each spec.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文