crudspec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ **.swp
5
+ tmp
6
+ spec
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.8.7@crudspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in crudspec.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ crudspec (0.0.1)
5
+ rails
6
+ rspec
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ abstract (1.0.0)
12
+ actionmailer (3.0.1)
13
+ actionpack (= 3.0.1)
14
+ mail (~> 2.2.5)
15
+ actionpack (3.0.1)
16
+ activemodel (= 3.0.1)
17
+ activesupport (= 3.0.1)
18
+ builder (~> 2.1.2)
19
+ erubis (~> 2.6.6)
20
+ i18n (~> 0.4.1)
21
+ rack (~> 1.2.1)
22
+ rack-mount (~> 0.6.12)
23
+ rack-test (~> 0.5.4)
24
+ tzinfo (~> 0.3.23)
25
+ activemodel (3.0.1)
26
+ activesupport (= 3.0.1)
27
+ builder (~> 2.1.2)
28
+ i18n (~> 0.4.1)
29
+ activerecord (3.0.1)
30
+ activemodel (= 3.0.1)
31
+ activesupport (= 3.0.1)
32
+ arel (~> 1.0.0)
33
+ tzinfo (~> 0.3.23)
34
+ activeresource (3.0.1)
35
+ activemodel (= 3.0.1)
36
+ activesupport (= 3.0.1)
37
+ activesupport (3.0.1)
38
+ arel (1.0.1)
39
+ activesupport (~> 3.0.0)
40
+ builder (2.1.2)
41
+ diff-lcs (1.1.2)
42
+ erubis (2.6.6)
43
+ abstract (>= 1.0.0)
44
+ i18n (0.4.2)
45
+ mail (2.2.9)
46
+ activesupport (>= 2.3.6)
47
+ i18n (~> 0.4.1)
48
+ mime-types (~> 1.16)
49
+ treetop (~> 1.4.8)
50
+ mime-types (1.16)
51
+ polyglot (0.3.1)
52
+ rack (1.2.1)
53
+ rack-mount (0.6.13)
54
+ rack (>= 1.0.0)
55
+ rack-test (0.5.6)
56
+ rack (>= 1.0)
57
+ rails (3.0.1)
58
+ actionmailer (= 3.0.1)
59
+ actionpack (= 3.0.1)
60
+ activerecord (= 3.0.1)
61
+ activeresource (= 3.0.1)
62
+ activesupport (= 3.0.1)
63
+ bundler (~> 1.0.0)
64
+ railties (= 3.0.1)
65
+ railties (3.0.1)
66
+ actionpack (= 3.0.1)
67
+ activesupport (= 3.0.1)
68
+ rake (>= 0.8.4)
69
+ thor (~> 0.14.0)
70
+ rake (0.8.7)
71
+ rspec (2.0.1)
72
+ rspec-core (~> 2.0.1)
73
+ rspec-expectations (~> 2.0.1)
74
+ rspec-mocks (~> 2.0.1)
75
+ rspec-core (2.0.1)
76
+ rspec-expectations (2.0.1)
77
+ diff-lcs (>= 1.1.2)
78
+ rspec-mocks (2.0.1)
79
+ rspec-core (~> 2.0.1)
80
+ rspec-expectations (~> 2.0.1)
81
+ thor (0.14.4)
82
+ treetop (1.4.8)
83
+ polyglot (>= 0.3.1)
84
+ tzinfo (0.3.23)
85
+
86
+ PLATFORMS
87
+ ruby
88
+
89
+ DEPENDENCIES
90
+ crudspec!
91
+ rails
92
+ rspec
data/README.markdown ADDED
@@ -0,0 +1,100 @@
1
+ # Crudspec
2
+
3
+ A RSpec spec generator for the basic CRUD controller.
4
+
5
+ This gem adds a generator to your app to generate a spec that will test
6
+ most of the cases for CRUD controllers.
7
+
8
+ I have tried it with Rails 3 and Rspec 2 and it works perfectly.
9
+ Hopfully later on I'll have some time to test other versions of Rails
10
+ and Rspec.
11
+
12
+ ## Installation
13
+
14
+ ### Rails 3
15
+
16
+ Include it on your Gemfile:
17
+
18
+ gem 'crudspec'
19
+
20
+ And install it
21
+
22
+ bundle install
23
+
24
+ Done.
25
+
26
+ ### Other versions of Rails
27
+
28
+ I still haven't tried other versions of Rails, if you do and it works,
29
+ please let me know.
30
+
31
+ ## Usage
32
+
33
+ Once it's on your Gemfile, you'll have a new Rails generator *rails
34
+ generate crudspec:spec*
35
+
36
+ If for example, you have a ProductsController, you'd generate your spec
37
+ by running:
38
+
39
+ rails generate crudspec:spec products_controller
40
+
41
+ This will generate *spec/controllers/products_controller_spec.rb*.
42
+
43
+ The spec will not be perfect, but it will give you a good head start.
44
+
45
+ A controller that would work out of the box would look something like:
46
+
47
+ class ProductsController < ApplicationController
48
+ def index
49
+ @products = Product.all
50
+ end
51
+
52
+ def new
53
+ @product = Product.new
54
+ end
55
+
56
+ def create
57
+ @product = Product.new(params[:product])
58
+ if @product.save
59
+ flash[:notice] = "Product was successfully created."
60
+ redirect_to product_url(@product)
61
+ else
62
+ render :action => :new
63
+ end
64
+ end
65
+
66
+ def edit
67
+ @product = Product.find(params[:id])
68
+ end
69
+
70
+ def update
71
+ @product = Product.find(params[:id])
72
+ if @product.update_attributes(params[:product])
73
+ flash[:notice] = "Product was successfully updated."
74
+ redirect_to product_path(@product)
75
+ else
76
+ render :action => :edit
77
+ end
78
+ end
79
+
80
+ def destroy
81
+ @product = Product.find(params[:id])
82
+ @product.delete
83
+ flash[:notice] = "Product was successfully destroyed."
84
+ redirect_to :products
85
+ end
86
+ end
87
+
88
+ # TODO
89
+
90
+ * Fix the tests! In theory they're properly written but they don't pass.
91
+ I give up.
92
+ * Option to create a test for Ruby::Test
93
+ * Option to create a cucumber feature
94
+ * Option to generate Specs that work out of the box with gems like [inherited_resources](https://github.com/josevalim/inherited_resources), [resource_controller](https://github.com/jamesgolick/resource_controller)
95
+
96
+ # About the Author
97
+
98
+ [Crowd Interactive](http://www.crowdint.com) is an American web design and development company that happens to work in Colima, Mexico.
99
+ We specialize in building and growing online retail stores. We don’t work with everyone – just companies we believe in. Call us today to see if there’s a fit.
100
+ Find more info [here](http://www.crowdint.com)!
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
data/crudspec.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "crudspec/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "crudspec"
7
+ s.version = Crudspec::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["David Padilla"]
10
+ s.email = ["david@crowdint.com"]
11
+ s.homepage = "http://rubygems.org/gems/crudspec"
12
+ s.summary = %q{Generate specs for CRUD controllers}
13
+ s.description = %q{Generate specs for CRUD controllers}
14
+
15
+ s.add_dependency('rails')
16
+ s.add_dependency('rspec')
17
+
18
+ s.rubyforge_project = "crudspec"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib", "generators"]
24
+ end
data/lib/crudspec.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'rails/generators'
2
+ require 'generators/spec_generator'
3
+
@@ -0,0 +1,3 @@
1
+ module Crudspec
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ module Crudspec
2
+ module Generators
3
+ class SpecGenerator < Rails::Generators::Base
4
+ argument :controller_name, :type => :string, :banner => 'controller_name'
5
+ source_root File.expand_path("../templates", __FILE__)
6
+
7
+ def generate_spec_file
8
+ underscored = controller_name.underscore
9
+ underscored = underscored + '_controller' unless underscored.match(/_controller$/)
10
+ @class_name = underscored.classify
11
+
12
+ # Really?
13
+ @model_name = @class_name.demodulize.match(/(.+)Controller$/)[1].underscore.singularize
14
+ file_path = "spec/controllers/#{underscored}_spec.rb"
15
+
16
+ template('controller_spec.rb', file_path)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= @class_name %> do
4
+ fixtures :<%= @model_name.pluralize %>
5
+
6
+ describe "GET 'index'" do
7
+ it "is successful" do
8
+ get :index
9
+ response.should be_success
10
+ end
11
+
12
+ it "assigns @<%= @model_name %>" do
13
+ get :index
14
+ assigns(:<%= @model_name.pluralize %>).should_not be_nil
15
+ end
16
+
17
+ it "renders index template" do
18
+ get :index
19
+ response.should render_template('index')
20
+ end
21
+ end
22
+
23
+ describe "GET 'new'" do
24
+ it 'is successful' do
25
+ get :new
26
+ response.should be_success
27
+ end
28
+
29
+ it "assigns @<%= @model_name %>" do
30
+ get :new
31
+ assigns(:<%= @model_name %>).should_not be_nil
32
+ end
33
+
34
+ it "renders the 'new' template" do
35
+ get :new
36
+ response.should render_template('new')
37
+ end
38
+ end
39
+
40
+ describe "POST 'create'" do
41
+ before(:each) do
42
+ @<%= @model_name %> = <%= @model_name.classify %>.new
43
+ @<%= @model_name %>.stub(:id).and_return(1)
44
+ end
45
+
46
+ context "The save is successful" do
47
+ before(:each) do
48
+ <%= @model_name.classify %>.should_receive(:new).and_return(@<%= @model_name %>)
49
+ @<%= @model_name %>.should_receive(:create).and_return(true)
50
+ end
51
+
52
+ it "redirects to the 'show' action" do
53
+ post :create, :<%= @model_name %> => @<%= @model_name %>.attributes
54
+ end
55
+
56
+ it "sets a flash message" do
57
+ post :create, :<%= @model_name %> => @<%= @model_name %>.attributes
58
+ flash[:notice].should == '<%= @model_name.classify %> was successfully created.'
59
+ end
60
+ end
61
+
62
+ context "the save fails" do
63
+ before(:each) do
64
+ @<%= @model_name %>.should_receive(:save).and_return(false)
65
+ <%= @model_name.classify %>.should_receive(:new).and_return(@<%= @model_name %>)
66
+ end
67
+
68
+ it "renders the 'new' action" do
69
+ post :create, :<%= @model_name %> => @<%= @model_name %>.attributes
70
+ response.should render_template(:new)
71
+ end
72
+
73
+ it "assigns @<%= @model_name %>" do
74
+ post :create, :<%= @model_name %> => @<%= @model_name %>.attributes
75
+ assigns(:<%= @model_name %>).should_not be_nil
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "GET 'edit'" do
81
+ before(:each) do
82
+ # Replace this with your Mock Factory, for ex: Machinist, Fabrication...
83
+ @<%= @model_name %> = <%= @model_name.pluralize %>(:one)
84
+ end
85
+
86
+ it 'is successful' do
87
+ get :edit, :id => @<%= @model_name %>.id
88
+ response.should be_success
89
+ end
90
+
91
+ it "assigns @<%= @model_name %>" do
92
+ get :edit, :id => @<%= @model_name %>.id
93
+ assigns(:<%= @model_name %>).should_not be_nil
94
+ end
95
+
96
+ it "renders the 'edit' template" do
97
+ get :edit, :id => @<%= @model_name %>.id
98
+ response.should render_template('edit')
99
+ end
100
+ end
101
+
102
+ describe "PUT 'update'" do
103
+ before(:each) do
104
+ # Replace this with your Mock Factory, for ex: Machinist, Fabrication...
105
+ @<%= @model_name %> = <%= @model_name.pluralize %>(:one)
106
+ end
107
+
108
+ context "the update is successful" do
109
+ before(:each) do
110
+ @<%= @model_name %>.should_receive(:update_attributes).and_return(true)
111
+ <%= @model_name.classify %>.should_receive(:find).with(@<%= @model_name %>.id).and_return(@<%= @model_name %>)
112
+ end
113
+
114
+ it "redirects to 'show' action" do
115
+ put :update, :id => @<%= @model_name %>.id, :<%= @model_name %> => {} # Add here some attributes for the model
116
+ response.should redirect_to(<%= @model_name %>_path(@<%= @model_name %>)) # Put the right show path here
117
+ end
118
+
119
+ it "sets a flash message" do
120
+ put :update, :id => @<%= @model_name %>.id, :<%= @model_name %> => {} # Add here some attributes for the model
121
+ flash[:notice].should == '<%= @model_name.classify %> was successfully updated.' # Your flash message here
122
+ end
123
+ end
124
+
125
+ context "the update fails" do
126
+ before(:each) do
127
+ @<%= @model_name %>.should_receive(:update_attributes).and_return(false)
128
+ <%= @model_name.classify %>.should_receive(:find).with(@<%= @model_name %>.id).and_return(@<%= @model_name %>)
129
+ end
130
+
131
+ it "renders the 'edit' action" do
132
+ put :update, :id => @<%= @model_name %>.id, :<%= @model_name %> => {} # Add here some attributes for the model
133
+ response.should render_template(:edit)
134
+ end
135
+
136
+ it "assigns @<%= @model_name %>" do
137
+ put :update, :id => @<%= @model_name %>.id, :<%= @model_name %> => {} # Add here some attributes for the model
138
+ assigns(:<%= @model_name %>).should_not be_nil
139
+ end
140
+ end
141
+ end
142
+
143
+ describe "DELETE 'destroy'" do
144
+ before(:each) do
145
+ # Replace this with your Mock Factory, for ex: Machinist, Fabrication...
146
+ @<%= @model_name %> = <%= @model_name.pluralize %>(:one)
147
+ <%= @model_name.classify %>.should_receive(:find).with(@<%= @model_name %>.id).and_return(@<%= @model_name %>)
148
+ end
149
+
150
+ it "should delete the <%= @model_name %>" do
151
+ @<%= @model_name %>.should_receive(:delete).and_return(true)
152
+ delete :destroy, :id => @<%= @model_name %>.id
153
+ end
154
+
155
+ it "should redirect to index page" do
156
+ delete :destroy, :id => @<%= @model_name %>.id
157
+ response.should redirect_to(:<%= @model_name.pluralize %>)
158
+ end
159
+
160
+ it "sets a flash message" do
161
+ delete :destroy, :id => @<%= @model_name %>.id
162
+ flash[:notice].should == '<%= @model_name.classify %> was successfully destroyed.' # Your flash message here
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class SpecGeneratorTest < Rails::Generators::TestCase
4
+ tests Crudspec::Generators::SpecGenerator
5
+ destination File.expand_path("../tmp", File.dirname(__FILE__))
6
+ setup :prepare_destination
7
+
8
+ test "create the controller files" do
9
+ Crudspec::Generators::SpecGenerator.start('admin/coupons_controller')
10
+ Crudspec::Generators::SpecGenerator.start('admin/discounts')
11
+ Crudspec::Generators::SpecGenerator.start('Admin::Products')
12
+ Crudspec::Generators::SpecGenerator.start('Admin::ClientsController')
13
+
14
+ assert_file 'spec/controllers/admin/coupons_controller_spec.rb'
15
+ assert_file 'spec/controllers/admin/discounts_controller_spec.rb'
16
+ assert_file 'spec/controllers/admin/products_controller_spec.rb'
17
+ assert_file 'spec/controllers/admin/clients_controller_spec.rb'
18
+ end
19
+ end
20
+
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'bundler/setup'
4
+ #require 'shoulda'
5
+ require 'crudspec'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crudspec
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - David Padilla
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-24 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: Generate specs for CRUD controllers
50
+ email:
51
+ - david@crowdint.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - .rvmrc
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - README.markdown
64
+ - Rakefile
65
+ - crudspec.gemspec
66
+ - lib/crudspec.rb
67
+ - lib/crudspec/version.rb
68
+ - lib/generators/spec_generator.rb
69
+ - lib/generators/templates/controller_spec.rb
70
+ - test/generators/test_spec_generator.rb
71
+ - test/test_helper.rb
72
+ has_rdoc: true
73
+ homepage: http://rubygems.org/gems/crudspec
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ - generators
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project: crudspec
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Generate specs for CRUD controllers
107
+ test_files:
108
+ - test/generators/test_spec_generator.rb
109
+ - test/test_helper.rb