Skip to content

Use keyword args for HTTP methods in controller specs only on Rails 5 #1710

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

Merged
merged 3 commits into from
Apr 11, 2017
Merged
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
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Enhancements:

* Add compatibility for Rails 5.1. (Sam Phippen, Yuichiro Kaneko, #1790)

Bug Fixes:

* Fix scaffold generator so that it does not generate broken controller specs
on Rails 3.x and 4.x. (Yuji Nakayama, #1710)

### 3.6.0.beta2 / 2016-12-12
[Full Changelog](http://github.com/rspec/rspec-rails/compare/v3.6.0.beta1...v3.6.0.beta2)

Expand Down
5 changes: 5 additions & 0 deletions example_app_generator/generate_stuff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ def using_source_path(path)
'config.warnings = false'
gsub_file '.rspec', '--warnings', ''

# Remove skips so we can test controller specs work
gsub_file 'spec/controllers/gadgets_controller_spec.rb',
'skip("Add a hash of attributes valid for your model")',
'{}'

final_tasks
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ def count
alias_method :size, :count
alias_method :length, :count

def last
all.last
end

def find(id)
id = id.to_i
all.find { |record| record.id == id } || raise
end

def create!(attributes = {})
with_id = { :id => next_id, :persisted => true }
all << record = new(with_id.merge(attributes))
record = new(attributes)
record.save
record
end

Expand All @@ -38,16 +47,50 @@ class Model
include ::ActiveModel::Validations

def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
assign_attributes(attributes)
end
end

attr_accessor :id, :persisted

alias_method :persisted?, :persisted

def update(attributes)
assign_attributes(attributes)
save
end

alias_method :update_attributes, :update

def assign_attributes(attributes)
attributes.each do |name, value|
__send__("#{name}=", value)
end
end

def save(*)
self.id = self.class.next_id
self.class.all << self
true
end

def destroy
self.class.all.delete(self)
true
end

def reload(*)
self
end

def ==(other)
other.is_a?(self.class) && id == other.id
end

def persisted?
!id.nil?
end

def new_record?
!persisted?
end
Expand Down
24 changes: 12 additions & 12 deletions lib/generators/rspec/scaffold/templates/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
describe "GET #index" do
it "returns a success response" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
get :index, {}, valid_session
<% else -%>
get :index, params: {}, session: valid_session
Expand All @@ -59,7 +59,7 @@
describe "GET #show" do
it "returns a success response" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
get :show, {:id => <%= file_name %>.to_param}, valid_session
<% else -%>
get :show, params: {id: <%= file_name %>.to_param}, session: valid_session
Expand All @@ -70,7 +70,7 @@

describe "GET #new" do
it "returns a success response" do
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
get :new, {}, valid_session
<% else -%>
get :new, params: {}, session: valid_session
Expand All @@ -82,7 +82,7 @@
describe "GET #edit" do
it "returns a success response" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
get :edit, {:id => <%= file_name %>.to_param}, valid_session
<% else -%>
get :edit, params: {id: <%= file_name %>.to_param}, session: valid_session
Expand All @@ -95,7 +95,7 @@
context "with valid params" do
it "creates a new <%= class_name %>" do
expect {
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
<% else -%>
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
Expand All @@ -104,7 +104,7 @@
end

it "redirects to the created <%= ns_file_name %>" do
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
post :create, {:<%= ns_file_name %> => valid_attributes}, valid_session
<% else -%>
post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session
Expand All @@ -115,7 +115,7 @@

context "with invalid params" do
it "returns a success response (i.e. to display the 'new' template)" do
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session
<% else -%>
post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session
Expand All @@ -133,7 +133,7 @@

it "updates the requested <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => new_attributes}, valid_session
<% else -%>
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session
Expand All @@ -144,7 +144,7 @@

it "redirects to the <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => valid_attributes}, valid_session
<% else -%>
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session
Expand All @@ -156,7 +156,7 @@
context "with invalid params" do
it "returns a success response (i.e. to display the 'edit' template)" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session
<% else -%>
put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session
Expand All @@ -170,7 +170,7 @@
it "destroys the requested <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
<% else -%>
delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
Expand All @@ -180,7 +180,7 @@

it "redirects to the <%= table_name %> list" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
<% if RUBY_VERSION < '1.9.3' -%>
<% if Rails::VERSION::STRING < '5.0' -%>
delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
<% else -%>
delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
Expand Down