工厂女工-目的是什么? [英] Factory Girl - what's the purpose?

查看:68
本文介绍了工厂女工-目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我可以使用before(:each)块时,在rspec测试中Factory Girl的目的是什么?好像Factory Girl和before(:each)之间的唯一区别是工厂在测试之外准备对象的创建.是这样吗?

What's the purpose of Factory Girl in rspec tests when I could use before(:each) blocks? It feels like the only difference between Factory Girl and a before(:each) is that the factory prepares object creation outside of the test. Is this right?

推荐答案

工厂女郎

Gems like Factory Girl and Sham allow you to create templates for valid and re-usable objects. They were created in response to fixtures which where fixed records that had to be loaded into the database. They allow more customization when you instantiate the objects and they aim to ensure that you 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)和after(:all)都旨在为您提供一个进行设置和拆卸的地方,这将在测试组之间共享.例如,如果要为每个测试创建一个新的有效用户,则需要在before(:each)挂钩中进行操作.如果要从文件系统中清除某些文件,则需要在前"钩子中执行此操作.如果所有测试都创建了一个tmp文件,并且您想在测试后将其删除,则可以在after(:each)或after(:all)钩子中进行操作.

before(:each), before(:all), after(:each) and after(:all) all aim to give you a place to do setup and teardown that will be shared amongst a test group. For example, if you are going to be creating a new valid user for every single test, then you'll want to do that in your before(:each) hook. If you are going to be clearing some files out of the filesystem, you want to do that in a before hook. If your tests all create a tmp file and you want to remove it after your test, you'll do that in your after(:each) or after(:all) hook.

这两个概念的区别在于工厂不是旨在围绕测试创建钩子,而是旨在创建有效的Ruby对象和记录,从而使您的对象创建灵活而又干燥.挂钩之前和之后的目标是在示例组中共享的设置和拆卸任务,这样您就可以保持设置和拆卸代码为DRY.

The ways these two concepts differ is that Factories are not aimed at creating hooks around your tests, they are aimed at creating valid Ruby objects and records, so that you can keep your object creation flexible and DRY. Before and after hooks are aimed at setup and teardown tasks that are shared in an example group so that you can keep your setup and teardown code DRY.

这篇关于工厂女工-目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆