具有多个关联的RSpec / FactoryGirl中的“名称已被使用”

ftshtw

我正在尝试运行非常基本的规格测试,但测试失败,并显示错误“名称已被占用”。

更新属于具有许多角色的用户

用户模型

# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string           default(""), not null
#

FactoryGirl.define do
  factory :user_engineer, class: User do
    id 1
    email '[email protected]'
    roles {[FactoryGirl.create(:engineer)]}
  end
end

角色模型

# == Schema Information
#
# Table name: roles
#
#  id          :integer          not null, primary key
#  name        :string
#  description :text
#

FactoryGirl.define do
  factory :engineer, class: Role do
    id 3
    name 'Engineer'
    description 'He is the chosen one'
  end
end

更新模型

# == Schema Information
#
# Table name: updates
#
#  id            :integer          not null, primary key
#  content       :text
#  user_id       :integer
#  ticket_id :integer
#

FactoryGirl.define do
  factory :update do
    content "This is a test update"
    association :user, factory: :user_engineer
  end
end

update_spec.rb

require 'rails_helper'

RSpec.describe Update, type: :model do
  let(:update){ FactoryGirl.create :update }
  it { expect(update).to be_valid }
end

这是错误:

Update
  example at ./spec/models/update_spec.rb:19 (FAILED - 1)

Failures:

  1) Update 
     Failure/Error: roles {[FactoryGirl.create(:engineer)]}

     ActiveRecord::RecordInvalid:
       Validation failed: Name has already been taken

我如何通过测试?

编辑:通过添加建议的序列行,运行后出现以下错误RAILS_ENV=test rake db:drop

1) Update 
     Failure/Error: roles {[FactoryGirl.create(:engineer)]}

     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "roles_pkey"
       DETAIL:  Key (id)=(3) already exists.
       : INSERT INTO "roles" ("id", "name", "description", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"
奥雅纳(Arup Rakshit)

从错误中可以很明显地看出,您属性进行了uniq验证name,因此应该使用sequence技术。

FactoryGirl.define do
  factory :engineer, class: Role do
    id 3
    sequence(:name) { |n| "Engineer-#{n}" }
    description 'He is the chosen one'
  end
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

association.first使用具有has_many关联的FactoryGirl工厂在RSpec功能规范中返回nil

来自分类Dev

association.first使用具有has_many关联的FactoryGirl工厂在RSpec功能规范中返回nil

来自分类Dev

删除熊猫数据框中具有多个关联的条目?

来自分类Dev

你如何制造可以在 rspec 中查询的具有 has_many 关联的对象?

来自分类Dev

使用用户输入在 ArrayList 中创建具有多个类关联的对象

来自分类Dev

在PL / SQL中从具有多个列的关联数组中删除重复项

来自分类Dev

在PL / SQL中从具有多个列的关联数组中删除重复项

来自分类Dev

使用simple_form构建具有多个关联输入的双嵌套表单

来自分类Dev

在同一个模型上使用一个具有多个关联并具有一个关联

来自分类Dev

在同一个模型上使用一个具有多个关联并具有一个关联

来自分类Dev

如何使用JMeter-Correlation Recorder将RegEx中具有多个匹配项的不同值相关联?

来自分类Dev

ORA-00955“名称已被现有对象使用”

来自分类Dev

具有has_many的Rspec FactoryGirl通过MissingAttributeError获得

来自分类Dev

使用FactoryGirl包含在验证和Rspec中

来自分类Dev

创建一个具有多个值的关联数组到php中的一个键

来自分类Dev

具有多个名称的功能

来自分类Dev

带有嵌套资源和关联的FactoryGirl对象的Controller的RSpec测试失败

来自分类Dev

禁用特征中的FactoryGirl关联

来自分类Dev

FactoryGirl中带有块的多个参数

来自分类Dev

如何查找具有给定名称空间的关联文件?

来自分类Dev

Rails:关联删除具有非标准名称的模型

来自分类Dev

如何查找具有给定名称空间的关联文件?

来自分类Dev

将键/值对从现有数组翻转到关联数组,该数组允许PHP中具有多个值的键

来自分类Dev

在CakePHP 3中使用其他关联创建具有条件的关联

来自分类Dev

FactoryGirl并获取具有所有关联的属性哈希

来自分类Dev

从关联的模型(表)中查找具有至少一个关联的所有记录

来自分类Dev

在MVC中处理具有相同名称的多个动作

来自分类Dev

Spring中具有相同名称的多个字段

来自分类Dev

在Linux中创建多个具有相同名称的文件

Related 相关文章

  1. 1

    association.first使用具有has_many关联的FactoryGirl工厂在RSpec功能规范中返回nil

  2. 2

    association.first使用具有has_many关联的FactoryGirl工厂在RSpec功能规范中返回nil

  3. 3

    删除熊猫数据框中具有多个关联的条目?

  4. 4

    你如何制造可以在 rspec 中查询的具有 has_many 关联的对象?

  5. 5

    使用用户输入在 ArrayList 中创建具有多个类关联的对象

  6. 6

    在PL / SQL中从具有多个列的关联数组中删除重复项

  7. 7

    在PL / SQL中从具有多个列的关联数组中删除重复项

  8. 8

    使用simple_form构建具有多个关联输入的双嵌套表单

  9. 9

    在同一个模型上使用一个具有多个关联并具有一个关联

  10. 10

    在同一个模型上使用一个具有多个关联并具有一个关联

  11. 11

    如何使用JMeter-Correlation Recorder将RegEx中具有多个匹配项的不同值相关联?

  12. 12

    ORA-00955“名称已被现有对象使用”

  13. 13

    具有has_many的Rspec FactoryGirl通过MissingAttributeError获得

  14. 14

    使用FactoryGirl包含在验证和Rspec中

  15. 15

    创建一个具有多个值的关联数组到php中的一个键

  16. 16

    具有多个名称的功能

  17. 17

    带有嵌套资源和关联的FactoryGirl对象的Controller的RSpec测试失败

  18. 18

    禁用特征中的FactoryGirl关联

  19. 19

    FactoryGirl中带有块的多个参数

  20. 20

    如何查找具有给定名称空间的关联文件?

  21. 21

    Rails:关联删除具有非标准名称的模型

  22. 22

    如何查找具有给定名称空间的关联文件?

  23. 23

    将键/值对从现有数组翻转到关联数组,该数组允许PHP中具有多个值的键

  24. 24

    在CakePHP 3中使用其他关联创建具有条件的关联

  25. 25

    FactoryGirl并获取具有所有关联的属性哈希

  26. 26

    从关联的模型(表)中查找具有至少一个关联的所有记录

  27. 27

    在MVC中处理具有相同名称的多个动作

  28. 28

    Spring中具有相同名称的多个字段

  29. 29

    在Linux中创建多个具有相同名称的文件

热门标签

归档