Skip to content

Commit

Permalink
Rails 5.0 - Updated tests to get announcements_controller working (#323)
Browse files Browse the repository at this point in the history
* Updated tests to get announcements_controller working

Changes:
1) Rails 4 -> 5, parameters no longer inherit from HashWithIndifferentAccess
2) response.json no longer is valid in Rails 5. response's class in Rails 4 is ActionController::TestResponse in Rails 5, response's class is ActionDispatch::TestResponse
3) Starting Rails 5, responses will default to 204s unless specified otherwise. And status of 204 will return response.content_type as nil

* update .json method  responses in test no longer an instance of ActionController::TestResponses

* update typo

* update comment

* ActionController::TestResponse injerits from ActionDispatch::TestResponse in Rails 4. Rails 5 test responses are instances of ActionDispatch::TestResponse

* update readme instructions to distinguish between bashing in dev environment vs test env
  • Loading branch information
yuenmichelle1 authored Jun 6, 2024
1 parent d1ac1c6 commit 3b19476
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ Some resources ([User](app/models/user.rb), [Subject](app/models/subject.rb)) ar
## Setting Up Rails-next
Using the gem https://github.com/clio/ten_years_rails to help with the upgrade path https://www.youtube.com/watch?v=6aCfc0DkSFo

### Using docker-compose for env setup
### Using docker-compose-rails-next for env setup

```
docker-compose -f docker-compose-rails-next.yml build
docker-compose -f docker-compose-rails-next.yml run --service-ports --rm talkapi bash
## To run bash in test env
docker-compose -f docker-compose-rails-next.yml run --service-ports --rm -e RAILS_ENV=test talkapi bash
```

### Install the gems via next
Expand Down
10 changes: 9 additions & 1 deletion app/services/concerns/talk_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ def permitted_params
end

def rooted_params
permitted_params.slice model_class.table_name
# Parameters in Rails 5+ behave differently, because they no longer inherit from HashWithIndifferentAccess
# methods like slice will behave differently in Rails 4 than Rails 5
# In Rails 5, we will need to first to apply to_h and then slice.
# TODO: Can Remove version comparison once Prod is on Rails 5+
if Rails.version.starts_with?('4.2')
permitted_params.slice model_class.table_name
else
permitted_params.to_h.slice model_class.table_name
end
end

def unrooted_params
Expand Down
2 changes: 1 addition & 1 deletion spec/support/api_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ActionController::TestResponse
class ActionDispatch::TestResponse
def json
@_json ||= JSON.parse(body).with_indifferent_access
end
Expand Down
5 changes: 3 additions & 2 deletions spec/support/shared_examples_for_controller_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
end

it 'should be json' do
expect(response.content_type).to eql 'application/json'
# starting from Rails 5+, if responding with 204, content-type of response is set by Rails as nil
expect(response.content_type).to eql 'application/json' unless response.status == 204
end

it 'should be an object' do
Expand Down Expand Up @@ -47,7 +48,7 @@
let!(:banned_ip){ create :user_ip_ban }
before(:each) do
allow(subject).to receive(:current_user).and_return user
allow(subject.request).to receive(:remote_ip).and_return '1.2.3.4'
request.env['REMOTE_ADDR'] = '1.2.3.4'
send_request
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/shared_examples_for_schedulable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

it 'gets queued on enqueued_times' do
enqueued_times.each do |enqueued_time|
# update to formatted_enqueue_time when updating sidekiq-cron to 1.9+ (sidekiq 7 support)
# TODO: Once on sidekiq-cron v1.9 can updated method to "formatted_enqueue_time"
job_enqueue_time = Time.at(job.formated_enqueue_time.to_f).utc
expect(job_enqueue_time).to eq(enqueued_time)
end
Expand Down

0 comments on commit 3b19476

Please sign in to comment.