次のコードでは、Rails 4.2アプリ(RSpec 3.5)およびShrine gem(ファイルアップロード用)のモデル仕様の画像検証をテストします。Rails:モデル仕様でのファイルアップロードの検証(Shrine gem)
私の質問は以下のとおりです。
- あなたは次のテストを改善する方法や、それらの検証をテストするためのより良い方法を考えることができますか?
- ファイルサイズ検証テストの速度を向上させるにはどうすればよいですか?可能であれば、実際に10MB以上のファイルをアップロードすることなくテストしたいと思います。
ファイルアップロードセットアップのその他の側面は、コントローラと機能仕様でテストされていますが、この質問とは関係ありません。 Rack::Test::UploadedFile
介してアップロードをルーティング、直接テストで直接固定具や工場等を用い添付メタデータレコードを作成するのではなく
RSpec.describe ShareImage, :type => :model do
describe "#image", :focus do
let(:image_file) do
# Could not get fixture_file_upload to work, but that's irrelevant
Rack::Test::UploadedFile.new(File.join(
ActionController::TestCase.fixture_path, 'files', filename))
end
let(:share_image) { FactoryGirl.build(:share_image, image: image_file) }
before(:each) { share_image.valid? }
context "with a valid image file" do
let(:filename) { 'image-valid.jpg' }
it "attaches the image to this record" do
expect(share_image.image.metadata["filename"]).to eq filename
end
end
context "with JPG extension and 'text/plain' media type" do
let(:filename) { 'image-with-text-media-type.jpg' }
it "is invalid" do
expect(share_image.errors[:image].to_s).to include("invalid file type")
end
end
# TODO: Refactor the following test (it takes ~50 seconds to run)
context "with a >10mb image file" do
let(:filename) { 'image-11mb.jpg' }
it "is invalid" do
expect(share_image.errors[:image].to_s).to include("too large")
end
end
end
end
これは素晴らしいです!どうもありがとうございました。 – BrunoFacca