我希望找出是否可以使用瞬态值更新对象属性,我有这个测试
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
运行测试时,我得到以下输出
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)>'
答案 0 :(得分:2)
一直在寻找错误的路线。你的工厂很好,你以正确的方式使用它。 Factory返回一个ActiveRecord对象,该对象不知道它是如何创建的,并且完全不知道它的工厂是如何生成的:
@image.update_attributes(categories_count: 1)
update_attributes
对记录本身采取行动,该记录本身不知道categories_count
。目前还不清楚你想要发生什么,或者你正在测试的是你正在测试FactoryGirl而不是你的模型。