rspec_generator 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +1 -0
- data/README +7 -0
- data/Rakefile +49 -0
- data/lib/controller_mixin.rb +160 -0
- data/lib/fixture_loading.rb +173 -0
- data/lib/rspec_on_rails.rb +67 -0
- data/rspec_generator.rb +34 -0
- data/templates/rspec.rake +27 -0
- data/templates/rspec_controller/USAGE +29 -0
- data/templates/rspec_controller/rspec_controller_generator.rb +41 -0
- data/templates/rspec_controller/templates/controller_spec.rb +22 -0
- data/templates/rspec_model/USAGE +18 -0
- data/templates/rspec_model/rspec_model_generator.rb +30 -0
- data/templates/rspec_model/templates/model_spec.rb +25 -0
- data/templates/spec_helper.rb +13 -0
- metadata +71 -0
data/CHANGES
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Please refer to the CHANGES file for RSpec's core
|
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require File.dirname(__FILE__) + '/../../../../../lib/spec/version'
|
6
|
+
|
7
|
+
desc 'Default: run unit tests.'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
|
11
|
+
PKG_NAME = 'rspec_generator'
|
12
|
+
PKG_VERSION = Spec::VERSION::STRING
|
13
|
+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
14
|
+
RELEASE_NAME = "REL #{PKG_VERSION}"
|
15
|
+
PKG_FILES = FileList[
|
16
|
+
"*.rb",
|
17
|
+
"lib/**/*",
|
18
|
+
"templates/**/*",
|
19
|
+
"[A-Z]*"
|
20
|
+
]
|
21
|
+
|
22
|
+
desc 'Generate documentation for the rspec_on_rails plugin.'
|
23
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = 'RSpecOnRails'
|
26
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README')
|
28
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
+
end
|
30
|
+
|
31
|
+
spec = Gem::Specification.new do |s|
|
32
|
+
s.name = PKG_NAME
|
33
|
+
s.version = PKG_VERSION
|
34
|
+
s.summary = "RSpec plugin and generator for Ruby on Rails"
|
35
|
+
s.has_rdoc = false
|
36
|
+
s.files = PKG_FILES
|
37
|
+
s.require_path = 'lib'
|
38
|
+
s.author = "Lachie Cox, Aslak Hellesoy"
|
39
|
+
s.email = "rspec-devel@rubyforge.org"
|
40
|
+
s.homepage = "http://rspec.rubyforge.org/"
|
41
|
+
s.add_dependency("rspec", "= #{PKG_VERSION}")
|
42
|
+
end
|
43
|
+
|
44
|
+
# Create Rubygem package
|
45
|
+
Rake::GemPackageTask.new(spec) do |p|
|
46
|
+
p.gem_spec = spec
|
47
|
+
p.need_tar = false
|
48
|
+
p.need_zip = false
|
49
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require 'rexml/document'
|
2
|
+
|
3
|
+
module Spec
|
4
|
+
module ControllerContext
|
5
|
+
def self.included(base)
|
6
|
+
super
|
7
|
+
base.send :include, Spec::ControllerContext::InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
def controller_name(name=nil)
|
12
|
+
@controller_name = name if name
|
13
|
+
@controller_name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ControllerExecution
|
19
|
+
|
20
|
+
def self.included(base)
|
21
|
+
super
|
22
|
+
base.send :include, Spec::ControllerExecution::InstanceMethods
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
|
27
|
+
attr_reader :response, :request, :controller
|
28
|
+
|
29
|
+
def setup_with_controller(controller_name=nil)
|
30
|
+
return unless controller_name
|
31
|
+
|
32
|
+
@controller_class = "#{controller_name}_controller".camelize.constantize
|
33
|
+
|
34
|
+
#@controller_class = Object.path2class @controller_class_name
|
35
|
+
raise "Can't determine controller class for #{self.class}" if @controller_class.nil?
|
36
|
+
|
37
|
+
@controller = @controller_class.new
|
38
|
+
|
39
|
+
@session = ActionController::TestSession.new
|
40
|
+
|
41
|
+
@flash = ActionController::Flash::FlashHash.new
|
42
|
+
@session['flash'] = @flash
|
43
|
+
|
44
|
+
@request = ActionController::TestRequest.new
|
45
|
+
@request.session = @session
|
46
|
+
|
47
|
+
@response = ActionController::TestResponse.new
|
48
|
+
@controller_class.send(:define_method, :rescue_action) { |e| raise e }
|
49
|
+
|
50
|
+
@deliveries = []
|
51
|
+
ActionMailer::Base.deliveries = @deliveries
|
52
|
+
|
53
|
+
# used by util_audit_assert_assigns
|
54
|
+
@assigns_asserted = []
|
55
|
+
@assigns_ignored ||= [] # untested assigns to ignore
|
56
|
+
end
|
57
|
+
|
58
|
+
def teardown_with_controller
|
59
|
+
end
|
60
|
+
|
61
|
+
def assigns(key = nil)
|
62
|
+
if key.nil?
|
63
|
+
@response.template.assigns
|
64
|
+
else
|
65
|
+
@response.template.assigns[key.to_s]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
##
|
71
|
+
# Excutes the request +action+ with +params+.
|
72
|
+
#
|
73
|
+
# See also: get, post, put, delete, head, xml_http_request
|
74
|
+
|
75
|
+
def process(action, parameters = nil)
|
76
|
+
parameters ||= {}
|
77
|
+
|
78
|
+
@request.recycle!
|
79
|
+
@request.env['REQUEST_METHOD'] ||= 'GET'
|
80
|
+
@request.action = action.to_s
|
81
|
+
|
82
|
+
@request.assign_parameters @controller_class.controller_path, action.to_s,
|
83
|
+
parameters
|
84
|
+
|
85
|
+
build_request_uri action, parameters
|
86
|
+
|
87
|
+
@controller.process @request, @response
|
88
|
+
end
|
89
|
+
|
90
|
+
##
|
91
|
+
# Performs a GET request on +action+ with +params+.
|
92
|
+
|
93
|
+
def get(action, parameters = nil)
|
94
|
+
@request.env['REQUEST_METHOD'] = 'GET'
|
95
|
+
process action, parameters
|
96
|
+
end
|
97
|
+
|
98
|
+
##
|
99
|
+
# Performs a HEAD request on +action+ with +params+.
|
100
|
+
|
101
|
+
def head(action, parameters = nil)
|
102
|
+
@request.env['REQUEST_METHOD'] = 'HEAD'
|
103
|
+
process action, parameters
|
104
|
+
end
|
105
|
+
|
106
|
+
##
|
107
|
+
# Performs a POST request on +action+ with +params+.
|
108
|
+
|
109
|
+
def post(action, parameters = nil)
|
110
|
+
@request.env['REQUEST_METHOD'] = 'POST'
|
111
|
+
process action, parameters
|
112
|
+
end
|
113
|
+
|
114
|
+
##
|
115
|
+
# Performs a PUT request on +action+ with +params+.
|
116
|
+
|
117
|
+
def put(action, parameters = nil)
|
118
|
+
@request.env['REQUEST_METHOD'] = 'PUT'
|
119
|
+
process action, parameters
|
120
|
+
end
|
121
|
+
|
122
|
+
##
|
123
|
+
# Performs a DELETE request on +action+ with +params+.
|
124
|
+
|
125
|
+
def delete(action, parameters = nil)
|
126
|
+
@request.env['REQUEST_METHOD'] = 'DELETE'
|
127
|
+
process action, parameters
|
128
|
+
end
|
129
|
+
|
130
|
+
def xml_document
|
131
|
+
@xml_document ||= REXML::Document.new(@response.body)
|
132
|
+
end
|
133
|
+
|
134
|
+
def xml_tags(xpath)
|
135
|
+
xml_document.elements.to_a(xpath)
|
136
|
+
end
|
137
|
+
|
138
|
+
def xml_attrs(xpath)
|
139
|
+
xml_document.elements.to_a(xpath).first.attributes
|
140
|
+
end
|
141
|
+
|
142
|
+
def xml_text(xpath)
|
143
|
+
xml_document.elements.to_a(xpath).first.text.to_s
|
144
|
+
end
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
def build_request_uri(action, parameters)
|
149
|
+
return if @request.env['REQUEST_URI']
|
150
|
+
|
151
|
+
options = @controller.send :rewrite_options, parameters
|
152
|
+
options.update :only_path => true, :action => action
|
153
|
+
|
154
|
+
url = ActionController::UrlRewriter.new @request, parameters
|
155
|
+
@request.set_REQUEST_URI url.rewrite(options)
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
|
2
|
+
module Spec
|
3
|
+
module Runner
|
4
|
+
|
5
|
+
class Specification
|
6
|
+
attr_accessor :loaded_fixtures
|
7
|
+
end
|
8
|
+
|
9
|
+
class Context
|
10
|
+
cattr_accessor :fixture_path
|
11
|
+
|
12
|
+
class_inheritable_accessor :fixture_table_names
|
13
|
+
class_inheritable_accessor :fixture_class_names
|
14
|
+
class_inheritable_accessor :use_transactional_fixtures
|
15
|
+
class_inheritable_accessor :use_instantiated_fixtures # true, false, or :no_instances
|
16
|
+
class_inheritable_accessor :pre_loaded_fixtures
|
17
|
+
|
18
|
+
attr_accessor :loaded_fixtures, :setup_block, :teardown_block
|
19
|
+
|
20
|
+
self.fixture_table_names = []
|
21
|
+
self.use_transactional_fixtures = false
|
22
|
+
self.use_instantiated_fixtures = true
|
23
|
+
self.pre_loaded_fixtures = false
|
24
|
+
|
25
|
+
self.fixture_class_names = {}
|
26
|
+
|
27
|
+
@@already_loaded_fixtures = {}
|
28
|
+
self.fixture_class_names = {}
|
29
|
+
|
30
|
+
def self.set_fixture_class(class_names = {})
|
31
|
+
self.fixture_class_names = self.fixture_class_names.merge(class_names)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.fixtures(*table_names)
|
35
|
+
table_names = table_names.flatten.map { |n| n.to_s }
|
36
|
+
self.fixture_table_names |= table_names
|
37
|
+
require_fixture_classes(table_names)
|
38
|
+
setup_fixture_accessors(table_names)
|
39
|
+
end
|
40
|
+
|
41
|
+
def fixtures(*table_names)
|
42
|
+
self.class.fixtures(*table_names)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.require_fixture_classes(table_names=nil)
|
46
|
+
(table_names || fixture_table_names).each do |table_name|
|
47
|
+
file_name = table_name.to_s
|
48
|
+
file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names
|
49
|
+
begin
|
50
|
+
require file_name
|
51
|
+
rescue LoadError
|
52
|
+
# Let's hope the developer has included it himself
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.setup_fixture_accessors(table_names=nil)
|
58
|
+
|
59
|
+
(table_names || fixture_table_names).each do |table_name|
|
60
|
+
table_name = table_name.to_s.tr('.','_')
|
61
|
+
|
62
|
+
# define_method(table_name)
|
63
|
+
# when defining the methods into Spec::Runner::ExecutionContext, you need to make sure
|
64
|
+
# it gets all the data it needs passed through somehow
|
65
|
+
# this is done in the lambda's in Spec::Runner::Context#run
|
66
|
+
Spec::Runner::ExecutionContext.send(:define_method, table_name) do |fixture, *optionals|
|
67
|
+
|
68
|
+
force_reload = optionals.shift
|
69
|
+
@fixture_cache[table_name] ||= Hash.new
|
70
|
+
@fixture_cache[table_name][fixture] = nil if force_reload
|
71
|
+
|
72
|
+
if @spec.loaded_fixtures[table_name][fixture.to_s]
|
73
|
+
@fixture_cache[table_name][fixture] ||= @spec.loaded_fixtures[table_name][fixture.to_s].find
|
74
|
+
else
|
75
|
+
raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.uses_transaction(*methods)
|
82
|
+
@uses_transaction ||= []
|
83
|
+
@uses_transaction.concat methods.map { |m| m.to_s }
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.uses_transaction?(method)
|
87
|
+
@uses_transaction && @uses_transaction.include?(method.to_s)
|
88
|
+
end
|
89
|
+
|
90
|
+
# instance
|
91
|
+
def use_transactional_fixtures?
|
92
|
+
use_transactional_fixtures
|
93
|
+
#&&
|
94
|
+
#!self.class.uses_transaction?(method_name)
|
95
|
+
end
|
96
|
+
|
97
|
+
def setup_with_fixtures
|
98
|
+
if pre_loaded_fixtures && use_transactional_fixtures?
|
99
|
+
raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures'
|
100
|
+
end
|
101
|
+
|
102
|
+
# @fixture_cache = Hash.new
|
103
|
+
|
104
|
+
# Load fixtures once and begin transaction.
|
105
|
+
if use_transactional_fixtures?
|
106
|
+
if @@already_loaded_fixtures[self.class]
|
107
|
+
@loaded_fixtures = @@already_loaded_fixtures[self.class]
|
108
|
+
else
|
109
|
+
load_fixtures
|
110
|
+
@@already_loaded_fixtures[self.class] = @loaded_fixtures
|
111
|
+
end
|
112
|
+
ActiveRecord::Base.lock_mutex
|
113
|
+
ActiveRecord::Base.connection.begin_db_transaction
|
114
|
+
|
115
|
+
# Load fixtures for every test.
|
116
|
+
else
|
117
|
+
@@already_loaded_fixtures[self.class] = nil
|
118
|
+
load_fixtures
|
119
|
+
end
|
120
|
+
|
121
|
+
# Instantiate fixtures for every test if requested.
|
122
|
+
instantiate_fixtures if use_instantiated_fixtures
|
123
|
+
end
|
124
|
+
|
125
|
+
def teardown_with_fixtures
|
126
|
+
# Rollback changes.
|
127
|
+
if use_transactional_fixtures?
|
128
|
+
ActiveRecord::Base.connection.rollback_db_transaction
|
129
|
+
ActiveRecord::Base.unlock_mutex
|
130
|
+
end
|
131
|
+
ActiveRecord::Base.verify_active_connections!
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
def load_fixtures
|
136
|
+
@loaded_fixtures = {}
|
137
|
+
fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names)
|
138
|
+
unless fixtures.nil?
|
139
|
+
if fixtures.instance_of?(Fixtures)
|
140
|
+
@loaded_fixtures[fixtures.table_name] = fixtures
|
141
|
+
else
|
142
|
+
fixtures.each { |f| @loaded_fixtures[f.table_name] = f }
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# for pre_loaded_fixtures, only require the classes once. huge speed improvement
|
148
|
+
@@required_fixture_classes = false
|
149
|
+
|
150
|
+
def instantiate_fixtures
|
151
|
+
if pre_loaded_fixtures
|
152
|
+
raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty?
|
153
|
+
unless @@required_fixture_classes
|
154
|
+
self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys
|
155
|
+
@@required_fixture_classes = true
|
156
|
+
end
|
157
|
+
Fixtures.instantiate_all_loaded_fixtures(self, load_instances?)
|
158
|
+
else
|
159
|
+
raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil?
|
160
|
+
@loaded_fixtures.each do |table_name, fixtures|
|
161
|
+
Fixtures.instantiate_fixtures(self, table_name, fixtures, load_instances?)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def load_instances?
|
167
|
+
use_instantiated_fixtures != :no_instances
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'application'
|
2
|
+
|
3
|
+
silence_warnings { RAILS_ENV = "test" }
|
4
|
+
|
5
|
+
require 'active_record/base'
|
6
|
+
require 'active_record/fixtures'
|
7
|
+
require 'action_controller/test_process'
|
8
|
+
require 'action_controller/integration'
|
9
|
+
require 'spec'
|
10
|
+
|
11
|
+
require File.dirname(__FILE__) + '/fixture_loading.rb'
|
12
|
+
require File.dirname(__FILE__) + '/controller_mixin.rb'
|
13
|
+
|
14
|
+
module Spec
|
15
|
+
module Runner
|
16
|
+
|
17
|
+
class ExecutionContext
|
18
|
+
include Spec::ControllerExecution
|
19
|
+
end
|
20
|
+
|
21
|
+
class Context
|
22
|
+
include Spec::ControllerContext
|
23
|
+
|
24
|
+
# entry point into rspec
|
25
|
+
# Keep it sync'ed!
|
26
|
+
def run(reporter,dry_run=false)
|
27
|
+
ctx = self
|
28
|
+
|
29
|
+
reporter.add_context(@name)
|
30
|
+
|
31
|
+
@specifications.each do |specification|
|
32
|
+
|
33
|
+
specification.run( reporter,
|
34
|
+
lambda do
|
35
|
+
@fixture_cache = Hash.new
|
36
|
+
ctx.setup_with_fixtures
|
37
|
+
setup_with_controller(ctx.controller_name)
|
38
|
+
|
39
|
+
specification.loaded_fixtures = ctx.loaded_fixtures
|
40
|
+
|
41
|
+
self.instance_exec(&ctx.setup_block) unless ctx.setup_block.nil?
|
42
|
+
end,
|
43
|
+
lambda do
|
44
|
+
self.instance_exec(&ctx.teardown_block) unless ctx.teardown_block.nil?
|
45
|
+
|
46
|
+
teardown_with_controller
|
47
|
+
ctx.teardown_with_fixtures
|
48
|
+
end,
|
49
|
+
dry_run
|
50
|
+
)
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def helper(name, &block)
|
56
|
+
self.class.helper(name, &block)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.helper(name, &block)
|
60
|
+
Spec::Runner::ExecutionContext.send :define_method, name.to_sym, &block
|
61
|
+
end
|
62
|
+
|
63
|
+
end # Context
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/rspec_generator.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# This generator adds the basic spec_helper.rb
|
2
|
+
# and rake tasks required to use RSpec with Rails
|
3
|
+
class RspecGenerator < Rails::Generator::Base
|
4
|
+
def initialize(runtime_args, runtime_options = {})
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
def manifest
|
9
|
+
record do |m|
|
10
|
+
# The spec helper and Rake tasks
|
11
|
+
m.directory 'spec'
|
12
|
+
m.template 'spec_helper.rb', 'spec/spec_helper.rb'
|
13
|
+
m.template 'rspec.rake', 'lib/tasks/rspec.rake'
|
14
|
+
|
15
|
+
# Copy out the rspec_model generator
|
16
|
+
m.directory 'vendor/generators/rspec_model/templates'
|
17
|
+
m.file 'rspec_model/USAGE', 'vendor/generators/rspec_model/USAGE'
|
18
|
+
m.file 'rspec_model/rspec_model_generator.rb', 'vendor/generators/rspec_model/rspec_model_generator.rb'
|
19
|
+
m.file 'rspec_model/templates/model_spec.rb', 'vendor/generators/rspec_model/templates/model_spec.rb'
|
20
|
+
|
21
|
+
# Copy out the rspec_controller generator
|
22
|
+
m.directory 'vendor/generators/rspec_controller/templates'
|
23
|
+
m.file 'rspec_controller/USAGE', 'vendor/generators/rspec_controller/USAGE'
|
24
|
+
m.file 'rspec_controller/rspec_controller_generator.rb', 'vendor/generators/rspec_controller/rspec_controller_generator.rb'
|
25
|
+
m.file 'rspec_controller/templates/controller_spec.rb', 'vendor/generators/rspec_controller/templates/controller_spec.rb'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
def banner
|
31
|
+
"Usage: #{$0} rspec"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec/rake/spectask'
|
2
|
+
|
3
|
+
desc 'Run all model and controller specs'
|
4
|
+
task :spec do
|
5
|
+
Rake::Task["spec:models"].invoke rescue got_error = true
|
6
|
+
Rake::Task["spec:controllers"].invoke rescue got_error = true
|
7
|
+
|
8
|
+
# not yet supported
|
9
|
+
#if File.exist?("spec/integration")
|
10
|
+
# Rake::Task["spec:integration"].invoke rescue got_error = true
|
11
|
+
#end
|
12
|
+
|
13
|
+
raise "RSpec failures" if got_error
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
namespace :spec do
|
18
|
+
desc "Run the specs under spec/models"
|
19
|
+
Spec::Rake::SpecTask.new(:models => "db:test:prepare") do |t|
|
20
|
+
t.spec_files = FileList['spec/models/**/*_spec.rb']
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Run the specs under spec/controllers"
|
24
|
+
Spec::Rake::SpecTask.new(:controllers => "db:test:prepare") do |t|
|
25
|
+
t.spec_files = FileList['spec/controllers/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Description:
|
2
|
+
The rspec_controller generator creates stubs for a new controller and its views.
|
3
|
+
|
4
|
+
The generator takes a controller name and a list of views as arguments.
|
5
|
+
The controller name may be given in CamelCase or under_score and should
|
6
|
+
not be suffixed with 'Controller'. To create a controller within a
|
7
|
+
module, specify the controller name as 'module/controller'.
|
8
|
+
|
9
|
+
The generator creates a controller class in app/controllers with view
|
10
|
+
templates in app/views/controller_name, a helper class in app/helpers,
|
11
|
+
and a functional spec suite in spec/functional.
|
12
|
+
|
13
|
+
Example:
|
14
|
+
./script/generate rspec_controller CreditCard open debit credit close
|
15
|
+
|
16
|
+
Credit card controller with URLs like /credit_card/debit.
|
17
|
+
Controller: app/controllers/credit_card_controller.rb
|
18
|
+
Views: app/views/credit_card/debit.rhtml [...]
|
19
|
+
Helper: app/helpers/credit_card_helper.rb
|
20
|
+
Spec: spec/controllers/credit_card_controller_spec.rb
|
21
|
+
|
22
|
+
Modules Example:
|
23
|
+
./script/generate controller 'admin/credit_card' suspend late_fee
|
24
|
+
|
25
|
+
Credit card admin controller with URLs /admin/credit_card/suspend.
|
26
|
+
Controller: app/controllers/admin/credit_card_controller.rb
|
27
|
+
Views: app/views/admin/credit_card/debit.rhtml [...]
|
28
|
+
Helper: app/helpers/admin/credit_card_helper.rb
|
29
|
+
Spec: spec/controllers/admin/credit_card_controller_spec.rb
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rails_generator/generators/components/controller/controller_generator'
|
2
|
+
|
3
|
+
class RspecControllerGenerator < ControllerGenerator
|
4
|
+
def manifest
|
5
|
+
record do |m|
|
6
|
+
# Check for class naming collisions.
|
7
|
+
m.class_collisions class_path, "#{class_name}Controller", "#{class_name}Helper"
|
8
|
+
|
9
|
+
# Controller, helper, views, and spec directories.
|
10
|
+
m.directory File.join('app/controllers', class_path)
|
11
|
+
m.directory File.join('app/helpers', class_path)
|
12
|
+
m.directory File.join('app/views', class_path, file_name)
|
13
|
+
m.directory File.join('spec/controllers', class_path)
|
14
|
+
|
15
|
+
# Controller class, functional spec, and helper class.
|
16
|
+
m.template 'controller:controller.rb',
|
17
|
+
File.join('app/controllers',
|
18
|
+
class_path,
|
19
|
+
"#{file_name}_controller.rb")
|
20
|
+
|
21
|
+
m.template 'controller_spec.rb',
|
22
|
+
File.join('spec/controllers',
|
23
|
+
class_path,
|
24
|
+
"#{file_name}_controller_spec.rb")
|
25
|
+
|
26
|
+
m.template 'controller:helper.rb',
|
27
|
+
File.join('app/helpers',
|
28
|
+
class_path,
|
29
|
+
"#{file_name}_helper.rb")
|
30
|
+
|
31
|
+
# View template for each action.
|
32
|
+
actions.each do |action|
|
33
|
+
path = File.join('app/views', class_path, file_name, "#{action}.rhtml")
|
34
|
+
m.template 'controller:view.rhtml',
|
35
|
+
path,
|
36
|
+
:assigns => { :action => action, :path => path }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper'
|
2
|
+
|
3
|
+
context "The <%= class_name %>Controller" do
|
4
|
+
# fixtures :<%= table_name %>
|
5
|
+
controller_name :<%= class_name.underscore %>
|
6
|
+
|
7
|
+
specify "should be a <%= class_name %>Controller" do
|
8
|
+
controller.should.be.an.instance.of <%= class_name %>Controller
|
9
|
+
end
|
10
|
+
|
11
|
+
<% for action in actions -%>
|
12
|
+
|
13
|
+
specify "should accept GET to <%= action %>"
|
14
|
+
get '<%= action %>'
|
15
|
+
response.should.be.success
|
16
|
+
end
|
17
|
+
<% end -%>
|
18
|
+
|
19
|
+
specify "should have more specifications" do
|
20
|
+
violated "not enough specs"
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Description:
|
2
|
+
The rspec_model generator creates stubs for a new model.
|
3
|
+
|
4
|
+
The generator takes a model name as its argument. The model name may be
|
5
|
+
given in CamelCase or under_score and should not be suffixed with 'Model'.
|
6
|
+
|
7
|
+
The generator creates a model class in app/models, an RSpec spec in
|
8
|
+
spec/models, database fixtures in spec/fixtures/plural_name.yml, and a migration
|
9
|
+
in db/migrate.
|
10
|
+
|
11
|
+
Example:
|
12
|
+
./script/generate rspec_model Account
|
13
|
+
|
14
|
+
This will create an Account model:
|
15
|
+
Model: app/models/account.rb
|
16
|
+
Spec: spec/models/account_spec.rb
|
17
|
+
Fixtures: spec/fixtures/accounts.yml
|
18
|
+
Migration: db/migrate/XXX_add_accounts.rb
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rails_generator/generators/components/model/model_generator'
|
2
|
+
|
3
|
+
class RspecModelGenerator < ModelGenerator
|
4
|
+
|
5
|
+
def manifest
|
6
|
+
|
7
|
+
record do |m|
|
8
|
+
# Check for class naming collisions.
|
9
|
+
m.class_collisions class_path, class_name
|
10
|
+
|
11
|
+
# Model, spec, and fixture directories.
|
12
|
+
m.directory File.join('app/models', class_path)
|
13
|
+
m.directory File.join('spec/models', class_path)
|
14
|
+
m.directory File.join('spec/fixtures', class_path)
|
15
|
+
|
16
|
+
# Model class, unit test, and fixtures.
|
17
|
+
m.template 'model:model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
18
|
+
m.template 'model:fixtures.yml', File.join('spec/fixtures', class_path, "#{table_name}.yml")
|
19
|
+
m.template 'model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
|
20
|
+
|
21
|
+
unless options[:skip_migration]
|
22
|
+
m.migration_template 'model:migration.rb', 'db/migrate', :assigns => {
|
23
|
+
:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
|
24
|
+
}, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper'
|
2
|
+
|
3
|
+
context "<%= class_name %> class with fixtures loaded" do
|
4
|
+
fixtures :<%= table_name %>
|
5
|
+
|
6
|
+
specify "should count two <%= class_name.pluralize %>" do
|
7
|
+
<%= class_name %>.count.should.be 2
|
8
|
+
end
|
9
|
+
|
10
|
+
specify "should have more specifications" do
|
11
|
+
violated "nothing specified"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "<%= class_name %> fixture :first" do
|
16
|
+
fixtures :<%= table_name %>
|
17
|
+
|
18
|
+
setup do
|
19
|
+
@first = <%= class_name.pluralize.underscore %>(:first)
|
20
|
+
end
|
21
|
+
|
22
|
+
specify "should have more specifications" do
|
23
|
+
violated "nothing specified"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
3
|
+
require 'rspec_on_rails'
|
4
|
+
|
5
|
+
module Spec
|
6
|
+
module Runner
|
7
|
+
class Context
|
8
|
+
self.use_transactional_fixtures = true
|
9
|
+
self.use_instantiated_fixtures = false
|
10
|
+
self.fixture_path = RAILS_ROOT + '/spec/fixtures'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: rspec_generator
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.5.5
|
7
|
+
date: 2006-06-06 00:00:00 -05:00
|
8
|
+
summary: RSpec plugin and generator for Ruby on Rails
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: rspec-devel@rubyforge.org
|
12
|
+
homepage: http://rspec.rubyforge.org/
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Lachie Cox, Aslak Hellesoy
|
30
|
+
files:
|
31
|
+
- rspec_generator.rb
|
32
|
+
- lib/controller_mixin.rb
|
33
|
+
- lib/fixture_loading.rb
|
34
|
+
- lib/rspec_on_rails.rb
|
35
|
+
- templates/rspec.rake
|
36
|
+
- templates/rspec_controller
|
37
|
+
- templates/rspec_model
|
38
|
+
- templates/spec_helper.rb
|
39
|
+
- templates/rspec_controller/rspec_controller_generator.rb
|
40
|
+
- templates/rspec_controller/templates
|
41
|
+
- templates/rspec_controller/USAGE
|
42
|
+
- templates/rspec_controller/templates/controller_spec.rb
|
43
|
+
- templates/rspec_model/rspec_model_generator.rb
|
44
|
+
- templates/rspec_model/templates
|
45
|
+
- templates/rspec_model/USAGE
|
46
|
+
- templates/rspec_model/templates/model_spec.rb
|
47
|
+
- CHANGES
|
48
|
+
- Rakefile
|
49
|
+
- README
|
50
|
+
test_files: []
|
51
|
+
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
executables: []
|
57
|
+
|
58
|
+
extensions: []
|
59
|
+
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
dependencies:
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec
|
65
|
+
version_requirement:
|
66
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.5.5
|
71
|
+
version:
|