はじめに
hkob の雑記録の第168回目は、ファイルプロパティへの File Upload object の対応をテスト・実装します。
FilesProperty#files=
FilesProperty にファイルを設定する files= メソッドを FileUploadObject に対応させます。まず、現状のテストを確認します。
context "when created by new" do let(:target) { described_class.new "fp", files: files } context "when no files" do let(:files) { [] } it_behaves_like "property values json", {fp: {type: "files", files: []}} describe "files=" do context "with a file url" do before { target.files = "f3" } it_behaves_like "property values json", { fp: { type: "files", files: [ { type: "external", name: "f3", external: { url: "f3", }, }, ], }, } it_behaves_like "will update" end
今回、files= に FileUploadObject を渡すことにします。
context "with a file upload object" do before { target.files = file_upload_object } it_behaves_like "property values json", { fp: { type: "files", files: [ { type: "file_upload", file_upload: { id: TestConnection::FILE_UPLOAD_IMAGE_ID, }, name: "test.png", }, ], }, } it_behaves_like "will update" end
file_upload_object はいつものように let で作成し、is_a? の stub を設定しておきます。今回は、fname の stub も設定しておきます。
let(:file_upload_object) do instance_double(FileUploadObject, id: TestConnection::FILE_UPLOAD_IMAGE_ID, fname: "test.png") end before { allow(file_upload_object).to receive(:is_a?).with(FileUploadObject).and_return(true) }
実装は以下のままで問題ありませんでした。これは昨日 FileObject#file_object を修正したためです。
def files=(files = []) assert_page_property __method__ @will_update = true @files = Array(files).map { |url| FileObject.file_object url } @file_names = Array(files) end
ただし、property_values_json の name だけがうまく動きませんでした。FileUploadObject の時だけ、name.fname を返すようにしました。
# @return [Hash] def property_values_json assert_page_property __method__ if @files.map(&:type).include? :file {} else files = @files.map(&:property_values_json) @file_names&.each_with_index do |name, i| files[i][:name] = name.is_a?(FileUploadObject) ? name.fname : name end {@name => {files: files, type: "files"}} end end
FileUploadObject が fname に応答するように attr_reader に追加しておきました。
attr_reader :id, :fname
これで無事にテストが通過しました。ついでに url と FileUploadObject を混ぜたものでも問題ないことを確認しました。
context "with a file url and a file upload object" do before { target.files = ["f3", file_upload_object] } it_behaves_like "property values json", { fp: { type: "files", files: [ { type: "external", name: "f3", external: { url: "f3", }, }, { type: "file_upload", name: "test.png", file_upload: { id: TestConnection::FILE_UPLOAD_IMAGE_ID, }, }, ], }, } it_behaves_like "will update" end
おわりに
FilesProperty の修正部分はほとんどないことがわかりました。今日は外出していてコードを書いている時間がなかったので、今日はここまでにしておきます。明日は実際にページのプロパティを変更するテストを実施します。