replace legacy timelog_controller#destroy specs

pull/6737/head
Jens Ulferts 6 years ago
parent f1b5c8f8fc
commit 9ac377484f
No known key found for this signature in database
GPG Key ID: 3CAA4B1182CF5308
  1. 61
      spec/controllers/timelog_controller_spec.rb
  2. 70
      spec_legacy/functional/timelog_controller_spec.rb

@ -33,7 +33,7 @@ describe TimelogController, type: :controller do
let(:project) { FactoryBot.create(:project) }
let(:user) do
FactoryBot.create(:admin,
member_in_project: project)
member_in_project: project)
end
let(:params) do
{ 'time_entry' => { 'work_package_id' => work_package_id,
@ -52,8 +52,6 @@ describe TimelogController, type: :controller do
describe '#create' do
shared_examples_for 'successful timelog creation' do
it { expect(response).to be_a_redirect }
it { expect(response).to redirect_to(project_time_entries_path(project)) }
end
@ -79,8 +77,8 @@ describe TimelogController, type: :controller do
context 'with a required custom field' do
let(:custom_field) do
FactoryBot.build_stubbed :time_entry_custom_field,
name: 'supplies',
is_required: true
name: 'supplies',
is_required: true
end
let(:time_entry) { double('time_entry', save: true, project: project) }
@ -111,7 +109,7 @@ describe TimelogController, type: :controller do
describe '#valid' do
let(:work_package) do
FactoryBot.create(:work_package,
project: project)
project: project)
end
let(:work_package_id) { work_package.id }
@ -139,4 +137,55 @@ describe TimelogController, type: :controller do
end
end
end
describe '#destroy' do
let(:time_entry) do
FactoryBot.build_stubbed(:time_entry).tap do |entry|
allow(TimeEntry)
.to receive(:find)
.with(entry.id.to_s)
.and_return(entry)
end
end
let(:expected_destroy_response) { true }
before do
expect(time_entry)
.to receive(:destroy)
.and_return(expected_destroy_response)
allow(time_entry)
.to receive(:destroyed?)
.and_return(expected_destroy_response)
delete :destroy, params: { id: time_entry.id }
end
context 'successful' do
it 'redirects to index' do
expect(response)
.to redirect_to project_time_entries_path(time_entry.project)
end
it 'returns with a success flash' do
expect(flash[:notice])
.to eql I18n.t(:notice_successful_delete)
end
end
context 'failure' do
let(:expected_destroy_response) { false }
it 'redirects to index' do
expect(response)
.to redirect_to project_time_entries_path(time_entry.project)
end
it 'returns with a success flash' do
expect(flash[:error])
.to eql I18n.t(:notice_unable_delete_time_entry)
end
end
end
end

@ -76,49 +76,6 @@ describe TimelogController, type: :controller do
assert_select 'option', content: '--- Please select ---'
end
it 'should post create' do
# TODO: should POST to issues’ time log instead of project. change form
# and routing
session[:user_id] = 3
post :create, params: { project_id: 1,
time_entry: { comments: 'Some work on TimelogControllerTest',
# Not the default activity
activity_id: '11',
spent_on: '2008-03-14',
work_package_id: '1',
hours: '7.3' } }
assert_redirected_to action: 'index', project_id: 'ecookbook'
i = WorkPackage.find(1)
t = TimeEntry.find_by(comments: 'Some work on TimelogControllerTest')
refute_nil t
assert_equal 11, t.activity_id
assert_equal 7.3, t.hours
assert_equal 3, t.user_id
assert_equal i, t.work_package
assert_equal i.project, t.project
end
it 'should post create with blank issue' do
# TODO: should POST to issues’ time log instead of project. change form
# and routing
session[:user_id] = 3
post :create, params: { project_id: 1,
time_entry: { comments: 'Some work on TimelogControllerTest',
# Not the default activity
activity_id: '11',
work_package_id: '',
spent_on: '2008-03-14',
hours: '7.3' } }
assert_redirected_to action: 'index', project_id: 'ecookbook'
t = TimeEntry.find_by(comments: 'Some work on TimelogControllerTest')
refute_nil t
assert_equal 11, t.activity_id
assert_equal 7.3, t.hours
assert_equal 3, t.user_id
end
it 'should update' do
entry = TimeEntry.find(1)
assert_equal 1, entry.work_package_id
@ -136,33 +93,6 @@ describe TimelogController, type: :controller do
assert_equal 2, entry.user_id
end
it 'should destroy' do
session[:user_id] = 2
delete :destroy, params: { id: 1 }
assert_redirected_to action: 'index', project_id: 'ecookbook'
assert_equal I18n.t(:notice_successful_delete), flash[:notice]
assert_nil TimeEntry.find_by(id: 1)
end
it 'should destroy should fail' do
# simulate that this fails (e.g. due to a plugin), see #5700
TimeEntry.class_eval do
before_destroy :stop_callback_chain
def stop_callback_chain
throw :abort
end
end
session[:user_id] = 2
delete :destroy, params: { id: 1 }
assert_redirected_to action: 'index', project_id: 'ecookbook'
assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
refute_nil TimeEntry.find_by(id: 1)
# remove the simulation
TimeEntry._destroy_callbacks.reject { |callback| callback.filter == :stop_callback_chain }
end
it 'should index all projects' do
get :index
assert_response :success

Loading…
Cancel
Save