Skip to content
This repository
Browse code

Merge pull request #78 from zesty/master

Update controller scaffold templates.
Thanks to @zesty for the submission!
  • Loading branch information...
commit 45b69dcf2ac2928159164ba537379148f9ea1763 2 parents fde9a17 + 1cec1f8
blowmage authored
15 lib/generators/mini_test/scaffold/scaffold_generator.rb
@@ -10,6 +10,8 @@ class ScaffoldGenerator < Base
10 10
11 11 check_class_collision :suffix => "ControllerTest"
12 12
  13 + argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
  14 +
13 15 def create_test_files
14 16 if options[:spec]
15 17 template "controller_spec.rb",
@@ -23,6 +25,19 @@ def create_test_files
23 25 "#{controller_file_name}_controller_test.rb")
24 26 end
25 27 end
  28 +
  29 + def accessible_attributes
  30 + attributes.reject(&:reference?)
  31 + end
  32 +
  33 + def attributes_hash
  34 + return if accessible_attributes.empty?
  35 +
  36 + accessible_attributes.map do |a|
  37 + name = a.name
  38 + key_value name, "@#{singular_table_name}.#{name}"
  39 + end.sort.join(', ')
  40 + end
26 41 end
27 42 end
28 43 end
12 lib/generators/mini_test/scaffold/templates/controller_spec.rb
@@ -4,7 +4,7 @@
4 4 describe <%= controller_class_name %>Controller do
5 5
6 6 before do
7   - @<%= singular_table_name %> = <%= class_name %>.new
  7 + @<%= singular_table_name %> = <%= table_name %>(:one)
8 8 end
9 9
10 10 it "must get index" do
@@ -20,30 +20,30 @@
20 20
21 21 it "must create <%= singular_table_name %>" do
22 22 assert_difference('<%= class_name %>.count') do
23   - post :create, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %>
  23 + post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
24 24 end
25 25
26 26 assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
27 27 end
28 28
29 29 it "must show <%= singular_table_name %>" do
30   - get :show, <%= key_value :id, "@#{singular_table_name}.to_param" %>
  30 + get :show, <%= key_value :id, "@#{singular_table_name}" %>
31 31 assert_response :success
32 32 end
33 33
34 34 it "must get edit" do
35   - get :edit, <%= key_value :id, "@#{singular_table_name}.to_param" %>
  35 + get :edit, <%= key_value :id, "@#{singular_table_name}" %>
36 36 assert_response :success
37 37 end
38 38
39 39 it "must update <%= singular_table_name %>" do
40   - put :update, <%= key_value :id, "@#{singular_table_name}.to_param" %>, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %>
  40 + put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
41 41 assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
42 42 end
43 43
44 44 it "must destroy <%= singular_table_name %>" do
45 45 assert_difference('<%= class_name %>.count', -1) do
46   - delete :destroy, <%= key_value :id, "@#{singular_table_name}.to_param" %>
  46 + delete :destroy, <%= key_value :id, "@#{singular_table_name}" %>
47 47 end
48 48
49 49 assert_redirected_to <%= index_helper %>_path
12 lib/generators/mini_test/scaffold/templates/controller_test.rb
@@ -4,7 +4,7 @@
4 4 class <%= controller_class_name %>ControllerTest < MiniTest::Rails::ActionController::TestCase
5 5
6 6 before do
7   - @<%= singular_table_name %> = <%= class_name %>.new
  7 + @<%= singular_table_name %> = <%= table_name %>(:one)
8 8 end
9 9
10 10 def test_index
@@ -20,30 +20,30 @@ def test_new
20 20
21 21 def test_create
22 22 assert_difference('<%= class_name %>.count') do
23   - post :create, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %>
  23 + post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
24 24 end
25 25
26 26 assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
27 27 end
28 28
29 29 def test_show
30   - get :show, <%= key_value :id, "@#{singular_table_name}.to_param" %>
  30 + get :show, <%= key_value :id, "@#{singular_table_name}" %>
31 31 assert_response :success
32 32 end
33 33
34 34 def test_edit
35   - get :edit, <%= key_value :id, "@#{singular_table_name}.to_param" %>
  35 + get :edit, <%= key_value :id, "@#{singular_table_name}" %>
36 36 assert_response :success
37 37 end
38 38
39 39 def test_update
40   - put :update, <%= key_value :id, "@#{singular_table_name}.to_param" %>, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %>
  40 + put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
41 41 assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
42 42 end
43 43
44 44 def test_destroy
45 45 assert_difference('<%= class_name %>.count', -1) do
46   - delete :destroy, <%= key_value :id, "@#{singular_table_name}.to_param" %>
  46 + delete :destroy, <%= key_value :id, "@#{singular_table_name}" %>
47 47 end
48 48
49 49 assert_redirected_to <%= index_helper %>_path

0 comments on commit 45b69dc

Please sign in to comment.
Something went wrong with that request. Please try again.