Skip to content

Commit

Permalink
Added a method helper for image specs to DRY code
Browse files Browse the repository at this point in the history
Commit History:
Changed file name to represent module name
Removed unnecessary line breaks
  • Loading branch information
cpfergus1 committed Apr 14, 2021
1 parent b99f955 commit 60b68e1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion core/spec/lib/search/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions core/spec/models/spree/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
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 }
let(:default_style) { :product }
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
Expand Down
7 changes: 7 additions & 0 deletions core/spec/support/image_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion core/spec/support/shared_examples/attachment.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 60b68e1

Please sign in to comment.