首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RSpec FactoryBot.lint在已排序的工厂名称上失败

RSpec FactoryBot.lint在已排序的工厂名称上失败
EN

Stack Overflow用户
提问于 2018-01-09 15:27:00
回答 1查看 1.5K关注 0票数 0

版本:

  • Rails 5.2.0.beta2
  • Ruby2.4.2p198 (2017-09-14修订版59899) x86_64-darwin17 17
  • rSpec 3.7.0

数据库清洁器和FactoryBot.lint在support/factory_bot.rb中一起运行

代码语言:javascript
运行
复制
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with :truncation

    begin
      DatabaseCleaner.start
      FactoryBot.lint strategy: :build unless config.files_to_run.one?
    ensure
      DatabaseCleaner.clean
    end
  end
end

运行bin/rspec将返回以下错误:

代码语言:javascript
运行
复制
jathayde$ bin/rspec

An error occurred in a `before(:suite)` hook.
Failure/Error: FactoryBot.lint strategy: :build unless config.files_to_run.one?

FactoryBot::InvalidFactoryError:
  The following factories are invalid:

  * project - Validation failed: Name has already been taken (ActiveRecord::RecordInvalid)
# ./spec/support/factory_bot.rb:10:in `block (2 levels) in <main>'


Finished in 0.60158 seconds (files took 2.66 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

这是项目工厂:

代码语言:javascript
运行
复制
FactoryBot.define do
  factory :project do
    sequence(:name) { |n| "Project-#{n}"}
    short_name { name.downcase.gsub(/[\s&\/]+/, "-") }
    association :category
    association :client
    page_title { name }
    meta_description "my text description"
  end
end

下面是models/project.rb文件:

代码语言:javascript
运行
复制
class Project < ApplicationRecord
  extend FriendlyId
  friendly_id :slug, use: [:slugged, :finders]

  belongs_to :client
  belongs_to :category

  validates :name,            presence: true
  validates :short_name,      presence: true,
                              uniqueness: true
  validates :category,        presence: true
  validates :client,          presence: {
                                on: :create,
                                message: "Must have a client for a project" }
  validates :page_title,      presence: true

  before_validation :set_slug

  private

  def set_slug
    self.slug = "#{name}".parameterize
  end
end

其他说明:

  • 无论uniqueness: true是否位于模型文件中的short_name上,都会发生这种情况。
  • 数据库架构中没有唯一的约束。
  • 如果我使用{Faker::Name.name}作为项目名称(没有序列),也会发生这种情况。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-09 16:05:16

原来这是category协会,而不是project本身--这才是问题所在。类别工厂:

代码语言:javascript
运行
复制
FactoryBot.define do
  factory :category do
    name "Name"
    short_name { Faker::Lorem.word }
    description { Faker::Lorem.paragraph }
  end
end

更改为此解决了以下问题:

代码语言:javascript
运行
复制
FactoryBot.define do
  factory :category do
    name { Faker::Name.name }
    short_name { Faker::Lorem.word }
    description { Faker::Lorem.paragraph }
  end
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48171478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档