Use the file_fixture helper in tests

This way we don't have to write `"spec/fixtures/files"` every time.

Note this method isn't included in factories. We could include it like
so:

```
FactoryBot::SyntaxRunner.class_eval do
  include ActiveSupport::Testing::FileFixtures
  self.file_fixture_path = RSpec.configuration.file_fixture_path
end
```

However, I'm not sure about the possible side effects, and since we only
use attachments in a few factories, there isn't much gain in applying
the monkey-patch.
This commit is contained in:
Javi Martín
2021-09-18 17:20:52 +02:00
parent 5ff66f96cd
commit 4f232c3a25
22 changed files with 73 additions and 80 deletions

View File

@@ -10,10 +10,10 @@ describe DirectUpload do
end
it "is not valid for different kind of combinations with invalid atttachment content types" do
expect(build(:direct_upload, :proposal, :documents, attachment: File.new("spec/fixtures/files/clippy.png"))).not_to be_valid
expect(build(:direct_upload, :proposal, :image, attachment: File.new("spec/fixtures/files/empty.pdf"))).not_to be_valid
expect(build(:direct_upload, :budget_investment, :documents, attachment: File.new("spec/fixtures/files/clippy.png"))).not_to be_valid
expect(build(:direct_upload, :budget_investment, :image, attachment: File.new("spec/fixtures/files/empty.pdf"))).not_to be_valid
expect(build(:direct_upload, :proposal, :documents, attachment: File.new(file_fixture("clippy.png")))).not_to be_valid
expect(build(:direct_upload, :proposal, :image, attachment: File.new(file_fixture("empty.pdf")))).not_to be_valid
expect(build(:direct_upload, :budget_investment, :documents, attachment: File.new(file_fixture("clippy.png")))).not_to be_valid
expect(build(:direct_upload, :budget_investment, :image, attachment: File.new(file_fixture("empty.pdf")))).not_to be_valid
end
it "is not valid without resource_type" do

View File

@@ -5,7 +5,7 @@ describe Document do
it_behaves_like "document validations", "proposal_document"
it "stores attachments with Active Storage" do
document = create(:document, attachment: File.new("spec/fixtures/files/clippy.pdf"))
document = create(:document, attachment: File.new(file_fixture("clippy.pdf")))
expect(document.attachment).to be_attached
expect(document.attachment.filename).to eq "clippy.pdf"

View File

@@ -5,7 +5,7 @@ describe Image do
it_behaves_like "image validations", "proposal_image"
it "stores attachments with Active Storage" do
image = create(:image, attachment: File.new("spec/fixtures/files/clippy.jpg"))
image = create(:image, attachment: File.new(file_fixture("clippy.jpg")))
expect(image.attachment).to be_attached
expect(image.attachment.filename).to eq "clippy.jpg"

View File

@@ -1,7 +1,7 @@
require "rails_helper"
describe LocalCensusRecords::Import do
let(:base_files_path) { %w[spec fixtures files local_census_records import] }
let(:base_files_path) { "local_census_records/import/" }
let(:import) { build(:local_census_records_import) }
describe "Validations" do
@@ -17,7 +17,7 @@ describe LocalCensusRecords::Import do
context "When file extension" do
it "is wrong it should not be valid" do
file = Rack::Test::UploadedFile.new("spec/fixtures/files/clippy.gif")
file = Rack::Test::UploadedFile.new(file_fixture("clippy.gif"))
import = build(:local_census_records_import, file: file)
expect(import).not_to be_valid
@@ -25,7 +25,7 @@ describe LocalCensusRecords::Import do
it "is csv it should be valid" do
path = base_files_path << "valid.csv"
file = Rack::Test::UploadedFile.new(Rails.root.join(*path))
file = Rack::Test::UploadedFile.new(file_fixture(path))
import = build(:local_census_records_import, file: file)
expect(import).to be_valid
@@ -35,7 +35,7 @@ describe LocalCensusRecords::Import do
context "When file headers" do
it "are all missing it should not be valid" do
path = base_files_path << "valid-without-headers.csv"
file = Rack::Test::UploadedFile.new(Rails.root.join(*path))
file = Rack::Test::UploadedFile.new(file_fixture(path))
import = build(:local_census_records_import, file: file)
expect(import).not_to be_valid
@@ -64,7 +64,7 @@ describe LocalCensusRecords::Import do
it "Add invalid local census records to invalid_records array" do
path = base_files_path << "invalid.csv"
file = Rack::Test::UploadedFile.new(Rails.root.join(*path))
file = Rack::Test::UploadedFile.new(file_fixture(path))
import.file = file
import.save!

View File

@@ -3,7 +3,7 @@ require "rails_helper"
describe SiteCustomization::Image do
it "stores images with Active Storage" do
image = create(:site_customization_image, name: "map",
image: File.new("spec/fixtures/files/custom_map.jpg"))
image: File.new(file_fixture("custom_map.jpg")))
expect(image.image).to be_attached
expect(image.image.filename).to eq "custom_map.jpg"
@@ -13,7 +13,7 @@ describe SiteCustomization::Image do
it "is valid with a 260x80 image" do
image = build(:site_customization_image,
name: "logo_header",
image: File.new("spec/fixtures/files/logo_header-260x80.png"))
image: File.new(file_fixture("logo_header-260x80.png")))
expect(image).to be_valid
end
@@ -21,7 +21,7 @@ describe SiteCustomization::Image do
it "is valid with a 223x80 image" do
image = build(:site_customization_image,
name: "logo_header",
image: File.new("spec/fixtures/files/logo_header.png"))
image: File.new(file_fixture("logo_header.png")))
expect(image).to be_valid
end
@@ -29,7 +29,7 @@ describe SiteCustomization::Image do
it "is not valid with a 400x80 image" do
image = build(:site_customization_image,
name: "logo_header",
image: File.new("spec/fixtures/files/logo_email_custom.png"))
image: File.new(file_fixture("logo_email_custom.png")))
expect(image).not_to be_valid
end