- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你知道我在这个测试中做错了什么吗?
require "rails_helper"
describe "Rep", type: :feature do
scenario "has a Manager and a Region" do
rep = FactoryGirl.create(:rep)
expect(rep.boss.email).to eq("first.last@email.com")
expect(rep.region.name).to eq("NAT")
end
end
给定区域的此类
class Region < ApplicationRecord
self.primary_key = "lid"
has_many :reps
end
我已更改区域的主键以引用列lid
FactoryGirl.define do
factory :rep do
lid 1
email '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="593f302b2a2d7735382a2d193c34383035773a3634" rel="noreferrer noopener nofollow">[email protected]</a>'
association :boss, strategy: :build
association :region, factory: :region, strategy: :build
end
factory :boss, class: Rep do
lid 2
email '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e3c2b3e60232f202f292b3c0e2b232f2722602d2123" rel="noreferrer noopener nofollow">[email protected]</a>'
manager true
association :region, factory: :region, strategy: :build
end
end
现在,当构建工厂中的关联区域
时,它会导致对该区域进行唯一性验证。
我使用这些来自另一个系统的称为旧版 ID (lid) 的 ID,并将它们用作主键(数据库迁移已设置为可以处理它们)。
我可以构建此应用程序特有的其他关联,因此具有标准的“id”字段
FactoryGirl.define do
factory :region do
lid 10
name 'NAT'
end
end
我想要的只是代表
和老板
都属于同一个地区
,所以当我建立一个代表时
工厂它带有 boss
和 region
关联。
希望这只是一些我没有考虑的愚蠢的事情:confused:
rspec spec spec/features/reps/have_managers.rb
F
Failures:
1) Reps belong to a manager
Failure/Error: rep = FactoryGirl.create(:rep)
ActiveRecord::RecordNotUnique:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_regions_on_lid"
DETAIL: Key (lid)=(10) already exists.
: INSERT INTO "regions" ("lid", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "lid"
# ./spec/features/reps/have_managers.rb:6:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# PG::UniqueViolation:
# ERROR: duplicate key value violates unique constraint "index_regions_on_lid"
# DETAIL: Key (lid)=(10) already exists.
# ./spec/features/reps/have_managers.rb:6:in `block (2 levels) in <top (required)>'
到目前为止,我已经关闭了 region_id null false flag
并将 belongs_to 设为可选
,这样我就可以注释掉区域关联并忽略它现在
最佳答案
我确信您已经弄清楚了这一点,但为了覆盖这一点,您的工厂中发生了一些重叠的事情。由于老板是代表,因此您不希望将老板工厂与代表工厂关联起来,否则您最终会得到任何重叠的重复条目。相反,您可以考虑使用特征:
FactoryGirl.define do
factory :rep, class: Rep do
sequence(:lid) { |n| n }
sequence(:email) { |e| "first.last#{e}@email.com" }
association :region, factory: :region
trait :boss do
manager true
end
end
end
然后,当您想要创建/构建这些时,您可以使用如下内容:
let(:rep) { create(:rep) }
let(:boss) { create(:rep, traits: [:boss]) }
关于ruby-on-rails - 更改模型的主键属性时,Rails FactoryGirl 重复键值违反唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43929757/
是否可以将一个关联用作特征中另一个关联的值? 让我描述一个(简化的)例子。我有 3 个模型 用户 论坛 (belongs_to :forum) 主题 (belongs_to :forum, belon
我正在尝试为我的 Image 对象的一些测试创建一些记录。但是,似乎它们并没有持续存在: describe Image do before(:all) do for i in 0.
我从事的项目结构如下: projects/warehouse/core projects/warehouse/products/bottles projects/warehouse/products/
我有两个相关模型——Customer,它有一个Address。这些是我的工厂: FactoryGirl.define do factory :customer do address e
我有一个使用 rspec 和 factorygirl 的 ruby 应用程序,但我在构建工厂时遇到了问题。当我运行规范时,我得到一个 ArgumentError: missing keywords
好的伙计们。这怎么说得通?两个嵌套的工厂(被 FactoryGirl 认为是继承)不应该相互冲突。到底他妈发生了什么?它要么不是继承,要么是。如果不是,我不知道为什么他们会称之为继承。我只是做错了什么
这可能很简单,但我在任何地方都找不到示例。 我有两个工厂: FactoryGirl.define do factory :profile do user title "direct
我在我的装置中使用 FactoryGirl,但我发现它并没有真正产生有用的验证错误。 我总是收到 activerecord.errors.models.messages.record_invalid
我有一个工厂,我在 factories/locations.rb 中定义了一个位置.我正在使用带有 ruby 1.9.3 的 Mongoid 和 Rails 3.1.1。 FactoryGirl.
我试图弄清楚如何编写属于 2 个不同模型的工厂,每个模型都应该具有相同的父模型。这是人为的示例代码: class User < ActiveRecord::Base has_many :widge
当我在开发中启动 Rails 控制台时,我看到 FactoryGirl 创建对象。显然我做错了,但是这样做的正确方法是什么?这段代码使我的测试工作...... # tests/factories/bo
我正在尝试与工厂女孩建立 has_many:has_many 关系。 这是我的模型: class User < ActiveRecord::Base has_many :user_roles
有没有办法拥有同一个工厂的多个版本?例如,user工厂。 FactoryGirl.define do factory :user#1 do name 'John Doe' date
您好,我正在使用带有 Rails 4.2.11 的 FactoryGirl 4.9.0。我向名为 Query 的模型添加了一个 bool 列,如下所示: class AddSetLatestResul
我在使用 FactoryGirl 时遇到问题,我正在使用序列来避免重复字段,但无论如何验证都失败了。 输出: 1) CustomersController anonymous user GET #
假设我有以下 ActiveRecord楷模: class Car belongs_to :driver end class Driver # Has attribute :name has
FactoryGirl 是否可以定义一个从 0 到 10 的随机数? factory :rating do ranking 1 #random number? re
我有一个标准的has_many关系(预订有很多订单),并确认没有至少一个订单不会保存预订。我正在尝试使用FactoryGirl工厂复制此文件,但是验证阻止我这样做。 class Booking < A
这些是2个简单的模型: class Post :destroy validates :asset, presence: true end class Asset post) end e
我有一个这样的代码模型工厂: Factory.define :code do |f| f.value "code" f.association :code_type f.ass
我是一名优秀的程序员,十分优秀!