rspec-rails 2.0.0.a5 → 2.0.0.a6

Sign up to get free protection for your applications and to get access to all the features.
@@ -34,7 +34,9 @@ Currently supported:
34
34
 
35
35
  * each example runs in its own transaction
36
36
  * not yet configurable
37
+ * i.e. no way to turn this off
37
38
  * model specs in spec/models
39
+ * controller specs in spec/controllers
38
40
  * request specs in spec/requests
39
41
  * these wrap rails integration tests
40
42
  * rails assertions
@@ -43,7 +45,6 @@ Currently supported:
43
45
 
44
46
  ### Known issues
45
47
 
46
- * no controller specs
47
48
  * no view specs
48
49
  * no helper specs
49
50
  * no routing specs
data/Rakefile CHANGED
@@ -18,7 +18,7 @@ begin
18
18
  gem.homepage = "http://github.com/rspec/rspec-rails"
19
19
  gem.authors = ["David Chelimsky", "Chad Humphries"]
20
20
  gem.add_dependency "rspec", ">= 2.0.0.a5"
21
- gem.add_dependency "webrat", "0.7.0"
21
+ gem.add_dependency "webrat", ">= 0.7.0"
22
22
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
23
23
  end
24
24
  Jeweler::GemcutterTasks.new
@@ -8,7 +8,7 @@ run('bundle install')
8
8
 
9
9
  run('script/rails g rspec:install')
10
10
  # run('script/rails g model thing name:string')
11
- run('script/rails g scaffold wombats name:string')
11
+ run('script/rails g scaffold widgets name:string')
12
12
  # run('script/rails g controller widgets index')
13
13
  # run('script/rails g integration_test widgets')
14
14
 
@@ -6,7 +6,7 @@ module Rspec
6
6
  argument :actions, :type => :array, :default => [], :banner => "action action"
7
7
 
8
8
  class_option :template_engine, :desc => "Template engine to generate view files"
9
- class_option :controllers, :type => :boolean, :default => false
9
+ class_option :controllers, :type => :boolean, :default => true
10
10
  class_option :views, :type => :boolean, :default => false
11
11
 
12
12
  def create_controller_files
@@ -6,7 +6,6 @@
6
6
  g.integration_tool :rspec
7
7
  g.test_framework :rspec,
8
8
  :fixture => false,
9
- :controllers => false,
10
9
  :views => false,
11
10
  :helpers => false,
12
11
  :routes => false,
@@ -11,7 +11,7 @@ module Rspec
11
11
  class_option :template_engine, :desc => "Template engine to generate view files"
12
12
  class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
13
13
 
14
- class_option :controllers, :type => :boolean, :default => false
14
+ class_option :controllers, :type => :boolean, :default => true
15
15
  class_option :views, :type => :boolean, :default => false
16
16
  class_option :routes, :type => :boolean, :default => false
17
17
 
@@ -3,40 +3,40 @@ require 'spec_helper'
3
3
  describe <%= controller_class_name %>Controller do
4
4
 
5
5
  def <%= mock_file_name %>(stubs={})
6
- @<%= mock_file_name %> ||= mock_model(<%= class_name %>, stubs)
6
+ @<%= mock_file_name %> ||= mock_model(<%= class_name %>, stubs).as_null_object
7
7
  end
8
8
 
9
9
  <% unless options[:singleton] -%>
10
10
  describe "GET index" do
11
11
  it "assigns all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
12
- <%= stub orm_class.all(class_name) %>.and_return([<%= mock_file_name %>])
12
+ <%= stub! orm_class.all(class_name) %>.and_return([<%= mock_file_name %>])
13
13
  get :index
14
- assigns[:<%= table_name %>].should == [<%= mock_file_name %>]
14
+ assigns(:<%= table_name %>).should == [<%= mock_file_name %>]
15
15
  end
16
16
  end
17
17
  <% end -%>
18
18
 
19
19
  describe "GET show" do
20
20
  it "assigns the requested <%= file_name %> as @<%= file_name %>" do
21
- <%= stub orm_class.find(class_name, "37".inspect) %>.and_return(<%= mock_file_name %>)
21
+ <%= stub! orm_class.find(class_name, "37".inspect) %>.and_return(<%= mock_file_name %>)
22
22
  get :show, :id => "37"
23
- assigns[:<%= file_name %>].should equal(<%= mock_file_name %>)
23
+ assigns(:<%= file_name %>).should equal(<%= mock_file_name %>)
24
24
  end
25
25
  end
26
26
 
27
27
  describe "GET new" do
28
28
  it "assigns a new <%= file_name %> as @<%= file_name %>" do
29
- <%= stub orm_class.build(class_name) %>.and_return(<%= mock_file_name %>)
29
+ <%= stub! orm_class.build(class_name) %>.and_return(<%= mock_file_name %>)
30
30
  get :new
31
- assigns[:<%= file_name %>].should equal(<%= mock_file_name %>)
31
+ assigns(:<%= file_name %>).should equal(<%= mock_file_name %>)
32
32
  end
33
33
  end
34
34
 
35
35
  describe "GET edit" do
36
36
  it "assigns the requested <%= file_name %> as @<%= file_name %>" do
37
- <%= stub orm_class.find(class_name, "37".inspect) %>.and_return(<%= mock_file_name %>)
37
+ <%= stub! orm_class.find(class_name, "37".inspect) %>.and_return(<%= mock_file_name %>)
38
38
  get :edit, :id => "37"
39
- assigns[:<%= file_name %>].should equal(<%= mock_file_name %>)
39
+ assigns(:<%= file_name %>).should equal(<%= mock_file_name %>)
40
40
  end
41
41
  end
42
42
 
@@ -44,13 +44,13 @@ describe <%= controller_class_name %>Controller do
44
44
 
45
45
  describe "with valid params" do
46
46
  it "assigns a newly created <%= file_name %> as @<%= file_name %>" do
47
- <%= stub orm_class.build(class_name, params) %>.and_return(<%= mock_file_name(:save => true) %>)
47
+ <%= stub! orm_class.build(class_name, params) %>.and_return(<%= mock_file_name(:save => true) %>)
48
48
  post :create, :<%= file_name %> => <%= params %>
49
- assigns[:<%= file_name %>].should equal(<%= mock_file_name %>)
49
+ assigns(:<%= file_name %>).should equal(<%= mock_file_name %>)
50
50
  end
51
51
 
52
52
  it "redirects to the created <%= file_name %>" do
53
- <%= stub orm_class.build(class_name) %>.and_return(<%= mock_file_name(:save => true) %>)
53
+ <%= stub! orm_class.build(class_name) %>.and_return(<%= mock_file_name(:save => true) %>)
54
54
  post :create, :<%= file_name %> => {}
55
55
  response.should redirect_to(<%= table_name.singularize %>_url(<%= mock_file_name %>))
56
56
  end
@@ -58,13 +58,13 @@ describe <%= controller_class_name %>Controller do
58
58
 
59
59
  describe "with invalid params" do
60
60
  it "assigns a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
61
- <%= stub orm_class.build(class_name, params) %>.and_return(<%= mock_file_name(:save => false) %>)
61
+ <%= stub! orm_class.build(class_name, params) %>.and_return(<%= mock_file_name(:save => false) %>)
62
62
  post :create, :<%= file_name %> => <%= params %>
63
- assigns[:<%= file_name %>].should equal(<%= mock_file_name %>)
63
+ assigns(:<%= file_name %>).should equal(<%= mock_file_name %>)
64
64
  end
65
65
 
66
66
  it "re-renders the 'new' template" do
67
- <%= stub orm_class.build(class_name) %>.and_return(<%= mock_file_name(:save => false) %>)
67
+ <%= stub! orm_class.build(class_name) %>.and_return(<%= mock_file_name(:save => false) %>)
68
68
  post :create, :<%= file_name %> => {}
69
69
  response.should render_template('new')
70
70
  end
@@ -76,19 +76,19 @@ describe <%= controller_class_name %>Controller do
76
76
 
77
77
  describe "with valid params" do
78
78
  it "updates the requested <%= file_name %>" do
79
- <%= should! orm_class.find(class_name, "37".inspect) %>.and_return(<%= mock_file_name %>)
80
- mock_<%= should! orm_instance.update_attributes(params) %>
79
+ <%= should_receive! orm_class.find(class_name, "37".inspect) %>.and_return(<%= mock_file_name %>)
80
+ mock_<%= should_receive! orm_instance.update_attributes(params) %>
81
81
  put :update, :id => "37", :<%= file_name %> => <%= params %>
82
82
  end
83
83
 
84
84
  it "assigns the requested <%= file_name %> as @<%= file_name %>" do
85
- <%= stub orm_class.find(class_name) %>.and_return(<%= mock_file_name(:update_attributes => true) %>)
85
+ <%= stub! orm_class.find(class_name) %>.and_return(<%= mock_file_name(:update_attributes => true) %>)
86
86
  put :update, :id => "1"
87
- assigns[:<%= file_name %>].should equal(<%= mock_file_name %>)
87
+ assigns(:<%= file_name %>).should equal(<%= mock_file_name %>)
88
88
  end
89
89
 
90
90
  it "redirects to the <%= file_name %>" do
91
- <%= stub orm_class.find(class_name) %>.and_return(<%= mock_file_name(:update_attributes => true) %>)
91
+ <%= stub! orm_class.find(class_name) %>.and_return(<%= mock_file_name(:update_attributes => true) %>)
92
92
  put :update, :id => "1"
93
93
  response.should redirect_to(<%= table_name.singularize %>_url(<%= mock_file_name %>))
94
94
  end
@@ -96,13 +96,13 @@ describe <%= controller_class_name %>Controller do
96
96
 
97
97
  describe "with invalid params" do
98
98
  it "assigns the <%= file_name %> as @<%= file_name %>" do
99
- <%= stub orm_class.find(class_name) %>.and_return(<%= mock_file_name(:update_attributes => false) %>)
99
+ <%= stub! orm_class.find(class_name) %>.and_return(<%= mock_file_name(:update_attributes => false) %>)
100
100
  put :update, :id => "1"
101
- assigns[:<%= file_name %>].should equal(<%= mock_file_name %>)
101
+ assigns(:<%= file_name %>).should equal(<%= mock_file_name %>)
102
102
  end
103
103
 
104
104
  it "re-renders the 'edit' template" do
105
- <%= stub orm_class.find(class_name) %>.and_return(<%= mock_file_name(:update_attributes => false) %>)
105
+ <%= stub! orm_class.find(class_name) %>.and_return(<%= mock_file_name(:update_attributes => false) %>)
106
106
  put :update, :id => "1"
107
107
  response.should render_template('edit')
108
108
  end
@@ -112,13 +112,13 @@ describe <%= controller_class_name %>Controller do
112
112
 
113
113
  describe "DELETE destroy" do
114
114
  it "destroys the requested <%= file_name %>" do
115
- <%= should! orm_class.find(class_name, "37".inspect) %>.and_return(<%= mock_file_name %>)
116
- mock_<%= should! orm_instance.destroy %>
115
+ <%= should_receive! orm_class.find(class_name, "37".inspect) %>.and_return(<%= mock_file_name %>)
116
+ mock_<%= should_receive! orm_instance.destroy %>
117
117
  delete :destroy, :id => "37"
118
118
  end
119
119
 
120
120
  it "redirects to the <%= table_name %> list" do
121
- <%= stub orm_class.find(class_name) %>.and_return(<%= mock_file_name(:destroy => true) %>)
121
+ <%= stub! orm_class.find(class_name) %>.and_return(<%= mock_file_name(:destroy => true) %>)
122
122
  delete :destroy, :id => "1"
123
123
  response.should redirect_to(<%= table_name %>_url)
124
124
  end
@@ -1,3 +1,4 @@
1
1
  require 'rspec/rails/transactional_database_support'
2
2
  require 'rspec/rails/matchers'
3
3
  require 'rspec/rails/example'
4
+ require 'rspec/rails/mocks'
@@ -20,12 +20,12 @@ module ControllerExampleGroupBehaviour
20
20
  eval <<-CODE
21
21
  def #{method}(*args)
22
22
  @_action = args.shift
23
- super '/'
23
+ super '/', *args
24
24
  end
25
25
  CODE
26
26
  end
27
27
 
28
28
  Rspec::Core.configure do |c|
29
- c.include self, :example_group => { :file_path => /\/spec\/controllers\// }
29
+ c.include self, :example_group => { :file_path => /\bspec\/controllers\// }
30
30
  end
31
31
  end
@@ -21,6 +21,6 @@ module RequestExampleGroupBehaviour
21
21
  end
22
22
 
23
23
  Rspec::Core.configure do |c|
24
- c.include self, :example_group => { :file_path => /\/spec\/requests\// }
24
+ c.include self, :example_group => { :file_path => /\bspec\/requests\// }
25
25
  end
26
26
  end
@@ -1,4 +1,5 @@
1
1
  require 'rspec/matchers'
2
+ require 'test/unit/assertionfailederror'
2
3
 
3
4
  module Rspec
4
5
  module Rails
@@ -6,11 +7,20 @@ module Rspec
6
7
  def redirect_to(destination)
7
8
  example = self
8
9
  Rspec::Matchers::Matcher.new :redirect_to, destination do |destination_|
9
- match do |_|
10
+ match_unless_raises Test::Unit::AssertionFailedError do |_|
10
11
  example.assert_redirected_to destination_
11
12
  end
12
13
  end
13
14
  end
15
+
16
+ def render_template(options={}, message=nil)
17
+ example = self
18
+ Rspec::Matchers::Matcher.new :render_template, options, message do |options_, message_|
19
+ match_unless_raises Test::Unit::AssertionFailedError do |_|
20
+ example.assert_template options_, message_
21
+ end
22
+ end
23
+ end
14
24
  end
15
25
  end
16
26
  end
@@ -0,0 +1,131 @@
1
+ module Rspec
2
+ module Rails
3
+
4
+ class IllegalDataAccessException < StandardError; end
5
+
6
+ module Mocks
7
+
8
+ # Creates a mock object instance for a +model_class+ with common
9
+ # methods stubbed out. Additional methods may be easily stubbed (via
10
+ # add_stubs) if +stubs+ is passed.
11
+ def mock_model(model_class, options_and_stubs = {})
12
+ id = options_and_stubs[:id] || next_id
13
+ options_and_stubs = options_and_stubs.reverse_merge({
14
+ :id => id,
15
+ :to_param => id.to_s,
16
+ :new_record? => false,
17
+ :destroyed? => false,
18
+ :errors => stub("errors", :count => 0)
19
+ })
20
+ derived_name = "#{model_class.name}_#{id}"
21
+ m = mock(derived_name, options_and_stubs)
22
+ m.__send__(:__mock_proxy).instance_eval <<-CODE
23
+ def @target.as_new_record
24
+ self.stub(:id) { nil }
25
+ self.stub(:to_param) { nil }
26
+ self.stub(:new_record?) { true }
27
+ self
28
+ end
29
+ def @target.is_a?(other)
30
+ #{model_class}.ancestors.include?(other)
31
+ end
32
+ def @target.kind_of?(other)
33
+ #{model_class}.ancestors.include?(other)
34
+ end
35
+ def @target.instance_of?(other)
36
+ other == #{model_class}
37
+ end
38
+ def @target.class
39
+ #{model_class}
40
+ end
41
+ def @target.to_s
42
+ "#{model_class.name}_#{id}"
43
+ end
44
+ CODE
45
+ yield m if block_given?
46
+ m
47
+ end
48
+
49
+ module ModelStubber
50
+ def connection
51
+ raise Spec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
52
+ end
53
+ def new_record?
54
+ id.nil?
55
+ end
56
+ def as_new_record
57
+ self.id = nil
58
+ self
59
+ end
60
+ end
61
+
62
+ # :call-seq:
63
+ # stub_model(Model)
64
+ # stub_model(Model).as_new_record
65
+ # stub_model(Model, hash_of_stubs)
66
+ # stub_model(Model, instance_variable_name, hash_of_stubs)
67
+ #
68
+ # Creates an instance of +Model+ that is prohibited from accessing the
69
+ # database*. For each key in +hash_of_stubs+, if the model has a
70
+ # matching attribute (determined by asking it) are simply assigned the
71
+ # submitted values. If the model does not have a matching attribute, the
72
+ # key/value pair is assigned as a stub return value using RSpec's
73
+ # mocking/stubbing framework.
74
+ #
75
+ # <tt>new_record?</tt> is overridden to return the result of id.nil?
76
+ # This means that by default new_record? will return false. If you want
77
+ # the object to behave as a new record, sending it +as_new_record+ will
78
+ # set the id to nil. You can also explicitly set :id => nil, in which
79
+ # case new_record? will return true, but using +as_new_record+ makes the
80
+ # example a bit more descriptive.
81
+ #
82
+ # While you can use stub_model in any example (model, view, controller,
83
+ # helper), it is especially useful in view examples, which are
84
+ # inherently more state-based than interaction-based.
85
+ #
86
+ # == Database Independence
87
+ #
88
+ # +stub_model+ does not make your examples entirely
89
+ # database-independent. It does not stop the model class itself from
90
+ # loading up its columns from the database. It just prevents data access
91
+ # from the object itself. To completely decouple from the database, take
92
+ # a look at libraries like unit_record or NullDB.
93
+ #
94
+ # == Examples
95
+ #
96
+ # stub_model(Person)
97
+ # stub_model(Person).as_new_record
98
+ # stub_model(Person, :id => 37)
99
+ # stub_model(Person) do |person|
100
+ # person.first_name = "David"
101
+ # end
102
+ def stub_model(model_class, stubs={})
103
+ stubs = {:id => next_id}.merge(stubs)
104
+ returning model_class.new do |m|
105
+ m.id = stubs.delete(:id)
106
+ m.extend ModelStubber
107
+ stubs.each do |k,v|
108
+ if m.has_attribute?(k)
109
+ m[k] = stubs.delete(k)
110
+ end
111
+ end
112
+ m.stub!(stubs)
113
+ yield m if block_given?
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ @@model_id = 1000
120
+
121
+ def next_id
122
+ @@model_id += 1
123
+ end
124
+
125
+ end
126
+ end
127
+ end
128
+
129
+ Rspec::Core.configure do |c|
130
+ c.include Rspec::Rails::Mocks
131
+ end
@@ -5,7 +5,7 @@ module Rspec # :nodoc:
5
5
  MAJOR = 2
6
6
  MINOR = 0
7
7
  TINY = 0
8
- PRE = 'a5'
8
+ PRE = 'a6'
9
9
 
10
10
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
11
11
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rspec-rails}
8
- s.version = "2.0.0.a5"
8
+ s.version = "2.0.0.a6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David Chelimsky", "Chad Humphries"]
12
- s.date = %q{2010-02-07}
12
+ s.date = %q{2010-02-08}
13
13
  s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.markdown"
@@ -56,6 +56,7 @@ Gem::Specification.new do |s|
56
56
  "lib/rspec/rails/example/controller_example_group.rb",
57
57
  "lib/rspec/rails/example/request_example_group.rb",
58
58
  "lib/rspec/rails/matchers.rb",
59
+ "lib/rspec/rails/mocks.rb",
59
60
  "lib/rspec/rails/transactional_database_support.rb",
60
61
  "lib/rspec/rails/version.rb",
61
62
  "rspec-rails.gemspec"
@@ -72,14 +73,14 @@ Gem::Specification.new do |s|
72
73
 
73
74
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
74
75
  s.add_runtime_dependency(%q<rspec>, [">= 2.0.0.a5"])
75
- s.add_runtime_dependency(%q<webrat>, ["= 0.7.0"])
76
+ s.add_runtime_dependency(%q<webrat>, [">= 0.7.0"])
76
77
  else
77
78
  s.add_dependency(%q<rspec>, [">= 2.0.0.a5"])
78
- s.add_dependency(%q<webrat>, ["= 0.7.0"])
79
+ s.add_dependency(%q<webrat>, [">= 0.7.0"])
79
80
  end
80
81
  else
81
82
  s.add_dependency(%q<rspec>, [">= 2.0.0.a5"])
82
- s.add_dependency(%q<webrat>, ["= 0.7.0"])
83
+ s.add_dependency(%q<webrat>, [">= 0.7.0"])
83
84
  end
84
85
  end
85
86
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.a5
4
+ version: 2.0.0.a6
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Chelimsky
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-07 00:00:00 -04:00
13
+ date: 2010-02-08 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -29,7 +29,7 @@ dependencies:
29
29
  version_requirement:
30
30
  version_requirements: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "="
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: 0.7.0
35
35
  version:
@@ -83,6 +83,7 @@ files:
83
83
  - lib/rspec/rails/example/controller_example_group.rb
84
84
  - lib/rspec/rails/example/request_example_group.rb
85
85
  - lib/rspec/rails/matchers.rb
86
+ - lib/rspec/rails/mocks.rb
86
87
  - lib/rspec/rails/transactional_database_support.rb
87
88
  - lib/rspec/rails/version.rb
88
89
  - rspec-rails.gemspec