Tag: factory bot

使用FactoryGirl测试简单的STI

我有一个类,这是其他一些专门处理行为的类的基础: class Task < ActiveRecord::Base attr_accessible :type, :name, :command validates_presence_of :type, :name, :command # some methods I would like to test end CounterTask类inheritance自Task class CounterTask < Task end 这一切都正常,直到我尝试测试基类,因为它必须有一个类型。 FactoryGirl.define do factory :task do sequence(:name) { |n| “name_#{n}” } sequence(:command) { |n| “command_#{n}” } end end 您如何测试超类的基本function?

什么是factory_girl瞬态属性? 我为什么要用一个?

我从Thoughtbot读到这个 ,但它仍然让我感到困惑。 这是他们的例子: factory :user do transient do rockstar true upcased false end name { “John Doe#{” – Rockstar” if rockstar}” } email { “#{name.downcase}@example.com” } after(:create) do |user, evaluator| user.name.upcase! if evaluator.upcased end end create(:user, upcased: true).name #=> “JOHN DOE – ROCKSTAR” 所以, .upcased是.upcased的真正属性吗? 什么是transient块真的在做什么? 设置可以在工厂中使用的变量? 什么是evaluator ? 它总是需要最后传递吗? 如果您的create函数使用特征,瞬态并具有多个值,该怎么办?

在每个示例之后,factory_girl + rspec似乎没有回滚更改

与此处描述的问题类似: http : //rpheath.com/posts/411-how-to-use-factory-girl-with-rspec 简而言之(缩短代码): spec_helper: config.use_transactional_fixtures = true config.use_instantiated_fixtures = false factories.rb: Factory.define :state do f.name “NY” end 在我的规范中 before(:each) do @static_model = Factory(:state) # with validate uniqueness of state name end 错误: 重复的条目名称“NY”等。 问题:在每个规范示例之前,不应该rspec清除数据库,因此不会抛出重复的条目错误吗?

如何重置factory_girl序列?

只要我有一个项目工厂 Factory.define :project do |p| p.sequence(:title) { |n| “project #{n} title” } p.sequence(:subtitle) { |n| “project #{n} subtitle” } p.sequence(:image) { |n| “../images/content/projects/#{n}.jpg” } p.sequence(:date) { |n| n.weeks.ago.to_date } end 而且我正在创建项目实例 Factory.build :project Factory.build :project 到这时,下次我执行Factory.build(:project)时,我将收到一个Project的实例,其标题设置为“project 3 title”,依此类推。 不奇怪。 现在说我希望在这个范围内重置我的计数器。 就像是: Factory.build :project #=> Project 3 Factory.reset :project #=> project factory counter gets reseted Factory.build […]

我如何分享我在创业板上的工厂并在其他项目中使用它?

我有一个包含一些工厂的gem。 gem看起来像: . ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── db ├── lib │ ├── models │ │ ├── users.rb ├── pkg ├── core.gemspec ├── spec │ ├── factories │ │ └── users.rb │ ├── fixtures │ ├── helpers │ ├── integration │ ├── spec_helper.rb │ ├── support│ │ │ └── unit │ […]