RSpec FactoryBot.lint 排序工厂名称失败
RSpec FactoryBot.lint fails on a sequenced factory name
版本:
- Rails 5.2.0.beta2
- ruby 2.4.2p198(2017-09-14 修订版 59899)[x86_64-darwin17]
- rSpec 3.7.0
数据库清理器和 FactoryBot.lint 运行 一起在 support/factory_bot.rb
:
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
returns 这个错误:
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
这是项目工厂:
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
文件:
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}
作为项目名称(没有序列)也会发生这种情况
原来这是 category
关联,而不是 project
本身的问题。类别工厂:
FactoryBot.define do
factory :category do
name "Name"
short_name { Faker::Lorem.word }
description { Faker::Lorem.paragraph }
end
end
改成这个解决了它:
FactoryBot.define do
factory :category do
name { Faker::Name.name }
short_name { Faker::Lorem.word }
description { Faker::Lorem.paragraph }
end
end
版本:
- Rails 5.2.0.beta2
- ruby 2.4.2p198(2017-09-14 修订版 59899)[x86_64-darwin17]
- rSpec 3.7.0
数据库清理器和 FactoryBot.lint 运行 一起在 support/factory_bot.rb
:
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
returns 这个错误:
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
这是项目工厂:
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
文件:
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}
作为项目名称(没有序列)也会发生这种情况
原来这是 category
关联,而不是 project
本身的问题。类别工厂:
FactoryBot.define do
factory :category do
name "Name"
short_name { Faker::Lorem.word }
description { Faker::Lorem.paragraph }
end
end
改成这个解决了它:
FactoryBot.define do
factory :category do
name { Faker::Name.name }
short_name { Faker::Lorem.word }
description { Faker::Lorem.paragraph }
end
end