diff --git a/lib/dsu/views/project/create.rb b/lib/dsu/views/project/create.rb index 3e7bed40..83496c20 100644 --- a/lib/dsu/views/project/create.rb +++ b/lib/dsu/views/project/create.rb @@ -29,6 +29,7 @@ def render end rescue StandardError => e puts apply_theme(e.message, theme_color: color_theme.error) + puts apply_theme(e.backtrace_locations.join("\n"), theme_color: color_theme.error) if Dsu.env.local? end private diff --git a/spec/dsu/views/project/create_spec.rb b/spec/dsu/views/project/create_spec.rb index 4f395153..709d5101 100644 --- a/spec/dsu/views/project/create_spec.rb +++ b/spec/dsu/views/project/create_spec.rb @@ -1,11 +1,15 @@ -# rubocop:disable RSpec/MultipleMemoizedHelpers # frozen_string_literal: true +# rubocop:disable RSpec/MultipleMemoizedHelpers RSpec.describe Dsu::Views::Project::Create do subject(:create_view) do described_class.new(presenter: presenter, options: options) end + before do + allow($stdin).to receive(:getch).and_return(response) + end + let(:presenter) do build(:create_presenter, project_name: project_name, description: description, options: options) end @@ -23,6 +27,53 @@ end describe '#render' do + context 'when the project already exists' do + let(:presenter) do + build(:create_presenter, :with_project, project_name: project_name, description: description, options: options) + end + let(:expected_error) do + "Project \"#{project_name}\" already exists." + end + + it 'displays the error' do + expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do + create_view.render + end.chomp)).to include(expected_error) + end + end + + context 'when the project does not exist' do + context "when the user confirmation is 'y'" do + let(:response) { 'y' } + + it 'displays the project created message' do + expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do + create_view.render + end.chomp)).to include("Created project \"#{project_name}\".") + end + + it 'creates the project' do + create_view.render + expect(Dsu::Models::Project.exist?(project_name: project_name)).to be(true) + end + end + + context "when the user confirmation is 'n'" do + let(:response) { 'n' } + + it 'displays the cancelled message' do + expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do + create_view.render + end.chomp)).to include('Cancelled.') + end + + it 'does not create the project' do + create_view.render + expect(Dsu::Models::Project.exist?(project_name: project_name)).to be(false) + end + end + end + context 'when the project has errors' do before do allow(presenter).to receive_messages(project_errors?: true, @@ -46,19 +97,17 @@ end end - context 'when the project already exists' do - # before do - # allow(presenter).to receive_messages(project_already_exists?: true) - # end + context 'when an error is raised that is not rescued' do + before do + allow(presenter).to receive(:project_errors?).and_raise(StandardError, expected_error) + end let(:presenter) do - build(:create_presenter, :with_project, project_name: project_name, description: description, options: options) - end - let(:expected_error) do - "Project \"#{project_name}\" already exists." + build(:create_presenter, project_name: project_name, description: description, options: options) end + let(:expected_error) { 'Test error' } - it 'displays the error' do + it 'rescues and displays the error' do expect(strip_escapes(Dsu::Services::StdoutRedirectorService.call do create_view.render end.chomp)).to include(expected_error) @@ -66,5 +115,4 @@ end end end - # rubocop:enable RSpec/MultipleMemoizedHelpers diff --git a/spec/dsu/views/project/use_spec.rb b/spec/dsu/views/project/use_spec.rb index 9240beaf..76c23941 100644 --- a/spec/dsu/views/project/use_spec.rb +++ b/spec/dsu/views/project/use_spec.rb @@ -30,7 +30,7 @@ build(:use_presenter, :with_project_name, project_name_or_number: project_name, options: options) end - context "when user the confirmation is 'y'" do + context "when the user confirmation is 'y'" do let(:response) { 'y' } it_behaves_like 'the project is not the current project' @@ -95,7 +95,7 @@ build(:use_presenter, :without_project_name, options: options) end - context "when user the confirmation is 'y'" do + context "when the user confirmation is 'y'" do let(:response) { 'y' } it_behaves_like 'the project is the current project' @@ -143,7 +143,7 @@ end end - context 'when an error is raised that is not captured' do + context 'when an error is raised that is not rescued' do before do allow(presenter).to receive(:project_does_not_exist?).and_raise(StandardError, expected_error) end