Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller Tests #970

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ group :test do
gem 'rspec-core'
gem 'rspec-rerun'
gem 'i18n-spec'
gem 'rails-controller-testing'
# code coverage
gem 'simplecov', require: false
gem 'simplecov-lcov', require: false
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ GEM
sprockets-rails (>= 2.0.0)
rails-assets-listjs (0.2.0.beta.4)
railties (>= 3.1)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand Down Expand Up @@ -606,6 +610,7 @@ DEPENDENCIES
rack-cors
rails (~> 5.2)
rails-assets-listjs (= 0.2.0.beta.4)
rails-controller-testing
rails-i18n
rails-settings-cached (= 0.4.3)
rails_tokeninput
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def reference_calculator
@bank_accounts = @types.includes(:bank_account).map(&:bank_account).uniq.compact
@bank_accounts = [BankAccount.last] if @bank_accounts.empty?
else
redirect_to root_url, alert: I18n.t('group_orders.errors.no_member')
redirect_to root_path, alert: I18n.t('group_orders.errors.no_member')
end
end

def update_profile
if @current_user.update(user_params)
@current_user.ordergroup.update(ordergroup_params) if ordergroup_params
session[:locale] = @current_user.locale
redirect_to my_profile_url, notice: I18n.t('home.changes_saved')
redirect_to my_profile_path, notice: I18n.t('home.changes_saved')
else
render :profile
end
Expand Down Expand Up @@ -64,7 +64,7 @@ def ordergroup
# cancel personal memberships direct from the myProfile-page
def cancel_membership
if params[:membership_id]
membership = @current_user.memberships.find!(params[:membership_id])
membership = @current_user.memberships.find(params[:membership_id])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it ok to remove the ! here and keep it in line 69?

Copy link
Member Author

@yksflip yksflip Dec 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think a find! method does not exists?

     NoMethodError:
       undefined method `find!' for #<ActiveRecord::Associations::CollectionProxy []>
       Did you mean?  find

find_by! returns a ActiveRecord::RecordNotFound instead of nil compared to find_by
I added another test to check that find also returns a ActiveRecord::RecordNotFound Exception

else
membership = @current_user.memberships.find_by_group_id!(params[:group_id])
end
Expand Down
12 changes: 12 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'spec_helper'

describe ApplicationController, type: :controller do
describe 'current' do
it 'returns current ApplicationController' do
described_class.new.send(:store_controller)
expect(described_class.current).to be_instance_of described_class
end
end
end
Loading