gpt4 book ai didi

ruby-on-rails - 如何测试依赖于文件的 before_create?

转载 作者:行者123 更新时间:2023-11-28 20:55:53 25 4
gpt4 key购买 nike

我的 Rails 应用模型中有以下代码:

class Image
before_create :choose_background
...

private

def choose_background
image = ChunkyPNG::Image.from_file(self.image_path)
background = ImageManipulator::get_background(image)
self.background = background unless background.nil?
end
end

现在我只有这些测试:

require 'test_helper'

class ImageTest < ActiveSupport::TestCase
test "should not save without a url" do
image = Image.new
assert !image.save
end

end

image_path 是通往 Image 路径的路径,而 ImageManipulator::get_background 基本上会进行一些图像处理以找到背景。所以,我的问题是:我应该如何测试这个?我应该在哪里/如何放置这些图像以测试该方法?

最佳答案

使用 FactoryGirl,我将创建一个图像,其图像路径引用我放置在/spec/fixtures/files 中的图像文件

测试本身看起来像

it "sets its background" do
image = create(:image)
expect(image.background).not_to be_nil
end

关于ruby-on-rails - 如何测试依赖于文件的 before_create?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20259088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com