From 60b68e1bcebf4beca10fff7bf02255502d231680 Mon Sep 17 00:00:00 2001 From: Connor Ferguson Date: Wed, 14 Apr 2021 07:13:55 -0600 Subject: [PATCH] Added a method helper for image specs to DRY code Commit History: Changed file name to represent module name Removed unnecessary line breaks --- core/spec/lib/search/base_spec.rb | 3 ++- core/spec/models/spree/image_spec.rb | 9 ++++----- core/spec/support/image_spec_helper.rb | 7 +++++++ core/spec/support/shared_examples/attachment.rb | 3 ++- 4 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 core/spec/support/image_spec_helper.rb diff --git a/core/spec/lib/search/base_spec.rb b/core/spec/lib/search/base_spec.rb index a5eab4bea9f..5b0423cc3d3 100644 --- a/core/spec/lib/search/base_spec.rb +++ b/core/spec/lib/search/base_spec.rb @@ -4,6 +4,7 @@ require 'spree/core/product_filters' RSpec.describe Spree::Core::Search::Base do + include ImageSpecHelper before do include Spree::Core::ProductFilters @taxon = create(:taxon, name: "Ruby on Rails") @@ -22,7 +23,7 @@ context "when include_images is included in the initialization params" do let(:params) { { include_images: true, keyword: @product1.name, taxon: @taxon.id } } - let(:image_file) { File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'blank.jpg')) } + let(:image_file) { open_image('blank.jpg') } subject { described_class.new(params).retrieve_products } before do diff --git a/core/spec/models/spree/image_spec.rb b/core/spec/models/spree/image_spec.rb index 96665e009bf..bcf1a58c50b 100644 --- a/core/spec/models/spree/image_spec.rb +++ b/core/spec/models/spree/image_spec.rb @@ -3,6 +3,7 @@ require 'rails_helper' RSpec.describe Spree::Image, type: :model do + include ImageSpecHelper it_behaves_like 'an attachment' do subject { create(:image) } let(:attachment_name) { :attachment } @@ -10,19 +11,17 @@ end it 'is valid when attachment has allowed content type' do - image = build(:image, - attachment: File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'blank.jpg'))) + image = build(:image, attachment: open_image('blank.jpg')) expect(image).to be_valid end it 'is not valid when attachment has restricted content type' do - image = build(:image, - attachment: File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'file.txt'))) + image = build(:image, attachment: open_image('file.txt')) expect(image).to_not be_valid end describe 'attachment details' do - let(:image_file) { File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'blank.jpg')) } + let(:image_file) { open_image('blank.jpg') } subject { create(:image, attachment: image_file) } it 'returns if attachment is present' do diff --git a/core/spec/support/image_spec_helper.rb b/core/spec/support/image_spec_helper.rb new file mode 100644 index 00000000000..7efba0a2af0 --- /dev/null +++ b/core/spec/support/image_spec_helper.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module ImageSpecHelper + def open_image(image) + File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', image)) + end +end diff --git a/core/spec/support/shared_examples/attachment.rb b/core/spec/support/shared_examples/attachment.rb index 72c678bfba4..e0abf64e26a 100644 --- a/core/spec/support/shared_examples/attachment.rb +++ b/core/spec/support/shared_examples/attachment.rb @@ -1,11 +1,12 @@ # frozen_string_literal: true RSpec.shared_examples 'an attachment' do + include ImageSpecHelper context 'valid attachment' do before do subject.send( :"#{attachment_name}=", - File.open(File.join('lib', 'spree', 'testing_support', 'fixtures', 'blank.jpg')) + open_image('blank.jpg') ) end