本文介绍了重设FactoryGirl测试的“序列"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何重置FactoryGirl的序列方法吗?

Does anyone know how to reset the sequence method for FactoryGirl?

我有一家工厂创建一个任务列表,我想每次定购从1开始.我使用序列"是因为任务列表是一个关联的模型,因此每次使用FactoryGirl.create时我都需要增加顺序,直到调用复位为止.

I have a factory that creates a list of tasks and I want to order to start at 1 every time. I use 'sequence' because the task list is a associated model, so I would need the order to increase every time I use FactoryGirl.create until I call a reset.

推荐答案

您需要在测试文件的回调之前/之后编写FactoryGirl.reload.

You need to write FactoryGirl.reload in the before/after callback of the test file.

示例代码段

  before do
    FactoryGirl.reload
    .
    .
    .
  end

."代表其他代码.

最好编写FactoryGirl.reload unless FactoryGirl.factories.blank?,以便在不需要时FactoryFactory不会重新加载.

Preferably write FactoryGirl.reload unless FactoryGirl.factories.blank? so that FactoryGirl does not reload, when it does not have to.

完全重装FactoryGirl可能会花费一些时间来处理测试;这也被称为反-模式.

Doing a full reload of FactoryGirl might have some overhead on the time it takes to process the test(s); this is also what is described as Anti-Patterns.

这篇关于重设FactoryGirl测试的“序列"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:49