Skip to content
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
30 changes: 0 additions & 30 deletions lib/generators/rspec/scaffold/scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,6 @@ def copy_view(view)
File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
end

def example_valid_attributes
# Only take the first attribute so this hash does not become unweildy and large in the
# generated controller spec. It is the responsibility of the user to keep the the valid
# attributes method up-to-date as they add validations.
@example_valid_attributes ||=
if attributes.any?
{ attributes.first.name => attributes.first.default.to_s }
else
{ }
end
end

def example_invalid_attributes
@example_invalid_attributes ||=
if attributes.any?
{ attributes.first.name => "invalid value" }
else
{ }
end
end

def example_params_for_update
@example_params_for_update ||=
if example_valid_attributes.any?
example_valid_attributes
else
{ "these" => "params" }
end
end

def formatted_hash(hash)
formatted = hash.inspect
formatted.gsub!("{", "{ ")
Expand Down
49 changes: 18 additions & 31 deletions lib/generators/rspec/scaffold/templates/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
# This should return the minimal set of attributes required to create a valid
# <%= class_name %>. As you add validations to <%= class_name %>, be sure to
# adjust the attributes here as well.
let(:valid_attributes) { <%= formatted_hash(example_valid_attributes) %> }
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}

let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
Expand Down Expand Up @@ -86,35 +92,28 @@

describe "with invalid params" do
it "assigns a newly created but unsaved <%= ns_file_name %> as @<%= ns_file_name %>" do
# Trigger the behavior that occurs when invalid params are submitted
allow_any_instance_of(<%= class_name %>).to receive(:save).and_return(false)
post :create, {:<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session
expect(assigns(:<%= ns_file_name %>)).to be_a_new(<%= class_name %>)
end

it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
allow_any_instance_of(<%= class_name %>).to receive(:save).and_return(false)
post :create, {:<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
post :create, {:<%= ns_file_name %> => invalid_attributes}, valid_session
expect(response).to render_template("new")
end
end
end

describe "PUT update" do
describe "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}

it "updates the requested <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
# Assuming there are no other <%= table_name %> in the database, this
# specifies that the <%= class_name %> created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
<%- if ::Rails::VERSION::STRING >= '4' -%>
expect_any_instance_of(<%= class_name %>).to receive(:update).with(<%= formatted_hash(example_params_for_update) %>)
<%- else -%>
expect_any_instance_of(<%= class_name %>).to receive(:update_attributes).with(<%= formatted_hash(example_params_for_update) %>)
<%- end -%>
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_params_for_update) %>}, valid_session
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => new_attributes}, valid_session
<%= file_name %>.reload
skip("Add assertions for updated state")
end

it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
Expand All @@ -133,25 +132,13 @@
describe "with invalid params" do
it "assigns the <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
<%- if ::Rails::VERSION::STRING >= '4' -%>
allow_any_instance_of(<%= class_name %>).to receive(:update).and_return(false)
<%- else -%>
allow_any_instance_of(<%= class_name %>).to receive(:update_attributes).and_return(false)
<%- end -%>
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session
expect(assigns(:<%= ns_file_name %>)).to eq(<%= file_name %>)
end

it "re-renders the 'edit' template" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
<%- if ::Rails::VERSION::STRING >= '4' -%>
allow_any_instance_of(<%= class_name %>).to receive(:update).and_return(false)
<%- else -%>
allow_any_instance_of(<%= class_name %>).to receive(:update_attributes).and_return(false)
<%- end -%>
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => <%= formatted_hash(example_invalid_attributes) %>}, valid_session
put :update, {:id => <%= file_name %>.to_param, :<%= ns_file_name %> => invalid_attributes}, valid_session
expect(response).to render_template("edit")
end
end
Expand Down
8 changes: 0 additions & 8 deletions spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
before { run_generator %w(posts) }
it { is_expected.to contain(/require 'spec_helper'/) }
it { is_expected.to contain(/describe PostsController, :type => :controller/) }
it { is_expected.to contain(%({ "these" => "params" })) }
end

describe 'with --no-controller_specs' do
Expand All @@ -23,13 +22,6 @@
end
end

describe 'controller spec with attributes specified' do
subject { file('spec/controllers/posts_controller_spec.rb') }
before { run_generator %w(posts title:string) }

it { is_expected.to contain(%({ "title" => "MyString" })) }
end

describe 'namespaced controller spec' do
subject { file('spec/controllers/admin/posts_controller_spec.rb') }
before { run_generator %w(admin/posts) }
Expand Down