Ruby on rails 使用瞬态Rspec/FactoryGirl更新_属性

Ruby on rails 使用瞬态Rspec/FactoryGirl更新_属性,ruby-on-rails,rspec,factory-bot,Ruby On Rails,Rspec,Factory Bot,我想知道是否有可能用一个瞬态值更新一个objects属性,我有这个测试 it 'should be able to update an image category' do @image = FactoryGirl.create(:image, categories_count: 2) expect(@image.image_categories.count).to eq(2) @image.update_attributes(categories_count: 1) @imag

我想知道是否有可能用一个瞬态值更新一个objects属性,我有这个测试

it 'should be able to update an image category' do
  @image = FactoryGirl.create(:image, categories_count: 2)
  expect(@image.image_categories.count).to eq(2)
  @image.update_attributes(categories_count: 1)
  @image.save
  expect(@image.image_categories.count).to eq(1)
end

FactoryGirl.define do
  factory :image do
    title 'Test Title'
    description 'Test Description'
    transient do
      photo_name 'validated_image.jpg'
    end
    photo { File.new(File.join(Rails.root, 'spec/fixtures', photo_name)) }
    transient do
      categories_count 1
    end
    categories { build_list(:category, categories_count) }
  end
end
但它失败了

ActiveRecord::UnknownAttributeError:
unknown attribute 'categories_count' for Image.
如何更新
@image

模式

create_table "images", force: :cascade do |t|
  t.string   "title"
  t.text     "description"
  t.string   "photo_file_name"
  t.string   "photo_content_type"
  t.integer  "photo_file_size"
  t.datetime "photo_updated_at"
  t.datetime "created_at",         null: false
  t.datetime "updated_at",         null: false
end

create_table "categories", force: :cascade do |t|
  t.string   "name"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "image_categories", force: :cascade do |t|
  t.integer  "image_id"
  t.integer  "category_id"
  t.datetime "created_at",  null: false
  t.datetime "updated_at",  null: false
end
模型

class Image < ActiveRecord::Base
  has_many :image_categories, dependent: :destroy
  has_many :categories, through: :image_categories
  has_attached_file :photo,
                styles: {
                  image_square_home: '350x350#',
                  image_thumb: '100x100#'
                }
  validates_attachment :photo, content_type: { :content_type => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png'], message: 'Images must be jpg/jpeg/gif/png format only' },
                           size: { in: 0..2000.kilobytes, message: 'Images should be less than 2 megabytes' }
  validates :title, presence: { message: "Don't forget to add a title" }
  validates :description, presence: { message: "Don't forget to add a description" }
  validates :categories, presence: { message: 'Choose At Least 1 Category' }
end

class Category < ActiveRecord::Base
  has_many :image_categories
  has_many :images, through: :image_categories
  validates :name, presence: { message: "Don't forget to add a Category" }
  validates_uniqueness_of :name, message: 'Category name %{value} already exists'
  validates_format_of :name, with: /\A([A-Za-z]+ )+[A-Za-z]+$|^[A-Za-z]+\z/, message: 'Only A-Z Characters Allowed', allow_blank: true, allow_nil: true
end 
类映像['image/jpg','image/jpeg','image/gif','image/png'],消息:'图像必须仅为jpg/jpeg/gif/png格式',
大小:{in:0..2000.KB,消息:“图像应小于2 MB”}
验证:title,presence:{message:“别忘了添加标题”}
验证:description,presence:{消息:“别忘了添加描述”}
验证:类别,状态:{消息:'至少选择一个类别'}
结束
类类别
运行测试时,我得到以下输出

 Failure/Error: @image.update_attributes(categories_count: 1)

 ActiveRecord::UnknownAttributeError:
   unknown attribute 'categories_count' for Image.
 # ./spec/models/image_spec.rb:71:in `block (4 levels) in <top (required)>'
 # ------------------
 # --- Caused by: ---
 # NoMethodError:
 #   undefined method `categories_count=' for #<Image:0x00000005483550>
 #   ./spec/models/image_spec.rb:71:in `block (4 levels) in <top (required)>'
失败/错误:@image.update\u属性(类别\u计数:1)
ActiveRecord::UnknownAttributeError:
图像的未知属性“categories\u count”。
#./spec/models/image_spec.rb:71:in'block(4层)in'
# ------------------
#---原因:---
#命名错误:
#未定义的方法“categories\u count=”用于#
#./spec/models/image_spec.rb:71:in'block(4层)in'

一直在看一条错误的线路。你的工厂很好,你用得对。Factory返回一个ActiveRecord对象,该对象不知道它是如何创建的,也完全不知道它的工厂是如何生成的:

@image.update_attributes(categories_count: 1)

update\u attributes
作用于记录本身,它不知道
categories\u count
。也不清楚您希望在这里发生什么,或者您实际测试的是什么,因为您测试的是FactoryGirl,而不是您的模型。

您是否可以添加您的数据库架构和
图像
类别
模型。@Tobias更新,thanksI希望有测试来检查
categories\u count
是否可以更新,因为图像可以有许多类别,所以我需要能够删除和删除add@Richlewis-你能把你的整个形象工厂都贴出来吗?@Broisasze更新,谢谢你的回答,我想测试的是,我可以更新我的
@image
对象的类别。所以当创建它时,它有2个,现在我想让它成为1,例如。。我该怎么做?