【问题标题】:Using machinist instead of fixtures使用机械师代替固定装置
【发布时间】:2010-12-09 12:30:47
【问题描述】:

在我的 Rails 3 应用程序中,我有一个 User 模型,其中包含以下字段

   name: string
   email: string
   children: has_many association to another model

我正在使用machinist 2生成模拟数据,它的蓝图看起来像

User.blueprint do
   name { 'user{sn}' }
   email { '{object.name}@domain.com' }
end

和用户的单元测试

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  should have_many( :children )
  should validate_uniqueness_of( :email )
  should_not allow_value("blah").for(:email)
  should_not allow_value("b lah").for(:email)
  should allow_value("a@b.com").for(:email)
  should allow_value("asdf@asdf.com").for(:email)
end

当我生成用户模型时,它创建了一个夹具文件。我的理解是,当我运行rake 时,Rails 使用该夹具文件来生成测试中使用的对象。这不是我想要的。 我希望 Rails 能够无缝地使用机械师的蓝图,因为它使用了夹具文件。

有没有办法做到这一点?有没有办法告诉 Rails 它需要使用蓝图而不是固定装置?

【问题讨论】:

    标签: unit-testing ruby-on-rails-3 fixtures machinist


    【解决方案1】:

    将此添加到 config/application.rb:

    config.generators do |g|
      g.fixture_replacement :machinist
    end
    

    你也可以安全地丢弃旧的灯具文件夹,除非你想明显地保留它们!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多