Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #14 from LazyStingray/master
Browse files Browse the repository at this point in the history
Added a generator for scaffolds to the shoulda generator
  • Loading branch information
Andre Arko committed May 11, 2011
2 parents d1a392a + 741b289 commit 04804d1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/generators/shoulda/scaffold/scaffold_generator.rb
@@ -0,0 +1,15 @@
require 'generators/shoulda'

module Shoulda
module Generators
class ScaffoldGenerator < Base
include Rails::Generators::ResourceHelpers

check_class_collision :suffix => 'ControllerTest'

def create_controller_file
template 'scaffold.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb")
end
end
end
end
70 changes: 70 additions & 0 deletions lib/generators/shoulda/scaffold/templates/scaffold.rb
@@ -0,0 +1,70 @@
require 'test_helper'

class <%= controller_class_name %>ControllerTest < ActionController::TestCase
context "index action" do
should "render index template" do
get :index
assert_template 'index'
end
end
context "show action" do
should "render show template" do
get :show, :id => <%= class_name %>.first
assert_template 'show'
end
end
context "new action" do
should "render new template" do
get :new
assert_template 'new'
end
end
context "create action" do
should "render new template when model is invalid" do
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
post :create
assert_template 'new'
end

should "redirect when model is valid" do
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
post :create
assert_redirected_to
end
end
context "edit action" do
should "render edit template" do
get :edit, :id => <%= class_name %>.first
assert_template 'edit'
end
end
context "update action" do
should "render edit template when model is invalid" do
<%= class_name %>.any_instance.stubs(:valid?).returns(false)
put :update, :id => <%= class_name %>.first
assert_template 'edit'
end
should "redirect when model is valid" do
<%= class_name %>.any_instance.stubs(:valid?).returns(true)
put :update, :id => <%= class_name %>.first
assert_redirected_to
end
end
context "destroy action" do
should "destroy model and redirect to index action" do
<%= singular_name %> = <%= class_name %>.first
delete :destroy, :id => <%= singular_name %>
assert_redirected_to
assert !<%= class_name %>.exists?(<%= singular_name %>.id)
end
end

end
37 changes: 37 additions & 0 deletions test/lib/generators/shoulda/scaffold_generator_test.rb
@@ -0,0 +1,37 @@
require 'test_helper'

class Shoulda::Generators::ScaffoldGeneratorTest < Rails::Generators::TestCase
destination File.join(Rails.root)
tests Rails::Generators::ScaffoldGenerator
arguments %w(accounts --test-framework shoulda)

setup :prepare_destination
setup :copy_routes

test "should invoke test framework" do
run_generator
assert_file "test/functional/accounts_controller_test.rb"
end

test "should create test class" do
run_generator
assert_file "test/functional/accounts_controller_test.rb" do |controller_test|
assert_class "AccountsControllerTest", controller_test
end
end

test "should create controller action tests" do
run_generator
assert_file "test/functional/accounts_controller_test.rb" do |controller_test|
assert_class "AccountsControllerTest", controller_test do |klass|
assert_match /context "index action"/, klass
assert_match /context "show action"/, klass
assert_match /context "new action"/, klass
assert_match /context "create action"/, klass
assert_match /context "edit action"/, klass
assert_match /context "update action"/, klass
assert_match /context "destroy action"/, klass
end
end
end
end
4 changes: 2 additions & 2 deletions test/test_helper.rb
Expand Up @@ -54,7 +54,7 @@ def generator_list
:factory_girl => ['model'],
:authlogic => ['session'],
:koala => ['install'],
:shoulda => ['controller']
:shoulda => ['controller', 'scaffold']
}
end

Expand All @@ -76,4 +76,4 @@ def require_generators(generator_list)
end
alias :require_generator :require_generators

require_generators generator_list
require_generators generator_list

0 comments on commit 04804d1

Please sign in to comment.