【发布时间】:2021-04-12 06:03:15
【问题描述】:
以下类正在进行测试,带有一个夹具和测试断言。
class Image < ApplicationRecord
belongs_to :user, optional: true
mount_uploader :image, ImageUploader
validates :image, presence: true
create_me:
brand_id: 1
image: MyString
caption: MyString
@image_create = images(:create_me)
assert_no_difference('Image.count') do
post images_url, xhr: true, params: { image: {brand_id: @image_create.brand_id, image: @image_create.image } }
end
运行此测试会返回错误,因为控制器会执行 .save!:
ActiveRecord::RecordInvalid: Validation failed: Image can't be blank
奇怪的是另一个类存在相同的模式并且相同的测试通过了。
class Attachment < ApplicationRecord
belongs_to :user, optional: true
mount_uploader :image, DocumentUploader
validates :image, presence: true
create_me:
brand_id: 1
image: MyString
caption: MyString
@attachment_create = attachments(:create_me)
assert_no_difference('Attachment.count') do
post attachments_url, xhr: true, params: { attachment: { caption: @attachment_create.caption, brand_id: @attachment_create.brand_id, image: @attachment_create.image } }
end
两个上传器类都按照手动测试中的设计运行。
我只能推测类名和属性名之间存在一些混淆,但这是一个疯狂的假设,因为手动测试运行正常。
是什么导致了这个失败?应该如何解决?
【问题讨论】:
-
你能分享测试吗?
-
我不确定我是否理解这个问题。测试在问题中给出(每个组类的第三块)。验证错误是通过带有
@image.save!的控制器行生成的
标签: ruby-on-rails testing