rspec-rails 2.0.0.beta.22 → 2.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.
data/History.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## rspec-rails release history (incomplete)
2
2
 
3
+ ### 2.0.0.rc / 2010-10-05
4
+
5
+ [full changelog](http://github.com/rspec/rspec-rails/compare/v2.0.0.beta.22...v2.0.0.rc)
6
+
7
+ * Enhancements
8
+ * add --webrat-matchers flag to scaffold generator (for view specs)
9
+ * separate ActiveModel and ActiveRecord APIs in mock_model and stub_model
10
+ * ControllerExampleGroup uses controller as the implicit subject by default (Paul Rosania)
11
+
3
12
  ### 2.0.0.beta.22 / 2010-09-12
4
13
 
5
14
  [full changelog](http://github.com/rspec/rspec-rails/compare/v2.0.0.beta.20...v2.0.0.beta.22)
@@ -0,0 +1,41 @@
1
+ class Rails < Thor
2
+ VERSIONS = {
3
+ :rails => {
4
+ "3.0.0" => "v3.0.0",
5
+ "master" => "master",
6
+ "3-0-stable" => "origin/3-0-stable"
7
+ },
8
+ :arel => {
9
+ "3.0.0" => "v1.0.0",
10
+ "master" => "master",
11
+ "3-0-stable" => "master"
12
+ }
13
+ }
14
+
15
+ desc "checkout VERSION", "checks it out (and arel)"
16
+ def checkout(version)
17
+ unless VERSIONS[:rails].has_key?(version)
18
+ raise "\n#{"*"*50}\nvalid versions are: #{VERSIONS[:rails].keys.join(", ")}\n#{"*"*50}\n"
19
+ end
20
+
21
+ puts "***** checking out rails at #{VERSIONS[:rails][version]} ..."
22
+ Dir.chdir("vendor/rails") do
23
+ `git checkout #{VERSIONS[:rails][version]}`
24
+ end
25
+
26
+ puts "***** checking out arel at #{VERSIONS[:arel][version]} ..."
27
+ Dir.chdir("vendor/arel") do
28
+ `git checkout #{VERSIONS[:arel][version]}`
29
+ end
30
+ end
31
+
32
+ desc "fetch", "update vendor/rails and vendor/arel"
33
+ def fetch
34
+ Dir.chdir("vendor/rails") do
35
+ `git fetch`
36
+ end
37
+ Dir.chdir("vendor/arel") do
38
+ `git fetch`
39
+ end
40
+ end
41
+ end
@@ -10,7 +10,7 @@ module Rspec
10
10
  class_option :view_specs, :type => :boolean, :default => true
11
11
 
12
12
  def create_controller_files
13
- return unless options[:controllers]
13
+ return unless options[:controller_specs]
14
14
 
15
15
  template 'controller_spec.rb',
16
16
  File.join('spec/controllers', class_path, "#{file_name}_controller_spec.rb")
@@ -18,7 +18,7 @@ module Rspec
18
18
 
19
19
  def create_view_files
20
20
  return if actions.empty?
21
- return unless options[:views]
21
+ return unless options[:view_specs]
22
22
 
23
23
  empty_directory File.join("spec", "views", file_path)
24
24
 
@@ -3,10 +3,10 @@ require 'generators/rspec'
3
3
  module Rspec
4
4
  module Generators
5
5
  class HelperGenerator < Base
6
- class_option :helpers, :type => :boolean, :default => true
6
+ class_option :helper_specs, :type => :boolean, :default => true
7
7
 
8
8
  def create_helper_files
9
- return unless options[:helpers]
9
+ return unless options[:helper_specs]
10
10
 
11
11
  template 'helper_spec.rb', File.join('spec/helpers', class_path, "#{file_name}_helper_spec.rb")
12
12
  end
@@ -14,6 +14,7 @@ module Rspec
14
14
 
15
15
  class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
16
16
  class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
17
+ class_option :webrat_matchers, :type => :boolean, :default => false, :desc => "Use webrat matchers in view specs"
17
18
  class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
18
19
  class_option :routing_specs, :type => :boolean, :default => true, :desc => "Generate routing specs"
19
20
 
@@ -49,6 +50,10 @@ module Rspec
49
50
 
50
51
  protected
51
52
 
53
+ def webrat?
54
+ options[:webrat_matchers] || @webrat_matchers_requested
55
+ end
56
+
52
57
  def copy_view(view)
53
58
  template "#{view}_spec.rb",
54
59
  File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
@@ -126,6 +131,10 @@ module Rspec
126
131
  end
127
132
  end
128
133
 
134
+ def banner
135
+ self.class.banner
136
+ end
137
+
129
138
  end
130
139
  end
131
140
  end
@@ -14,10 +14,19 @@ describe "<%= table_name %>/edit.html.<%= options[:template_engine] %>" do
14
14
  it "renders the edit <%= file_name %> form" do
15
15
  render
16
16
 
17
+ <% if webrat? -%>
17
18
  rendered.should have_selector("form", :action => <%= file_name %>_path(@<%= file_name %>), :method => "post") do |form|
18
19
  <% for attribute in output_attributes -%>
19
20
  form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
20
21
  <% end -%>
21
22
  end
23
+ <% else -%>
24
+ # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
25
+ assert_select "form", :action => <%= file_name %>_path(@<%= file_name %>), :method => "post" do
26
+ <% for attribute in output_attributes -%>
27
+ assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]"
28
+ <% end -%>
29
+ end
30
+ <% end -%>
22
31
  end
23
32
  end
@@ -19,7 +19,12 @@ describe "<%= table_name %>/index.html.<%= options[:template_engine] %>" do
19
19
  it "renders a list of <%= table_name %>" do
20
20
  render
21
21
  <% for attribute in output_attributes -%>
22
+ <% if webrat? -%>
22
23
  rendered.should have_selector("tr>td", :content => <%= value_for(attribute) %>.to_s, :count => 2)
24
+ <% else -%>
25
+ # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
26
+ assert_select "tr>td", :text => <%= value_for(attribute) %>.to_s, :count => 2
27
+ <% end -%>
23
28
  <% end -%>
24
29
  end
25
30
  end
@@ -14,10 +14,19 @@ describe "<%= table_name %>/new.html.<%= options[:template_engine] %>" do
14
14
  it "renders new <%= file_name %> form" do
15
15
  render
16
16
 
17
+ <% if webrat? -%>
17
18
  rendered.should have_selector("form", :action => <%= table_name %>_path, :method => "post") do |form|
18
19
  <% for attribute in output_attributes -%>
19
20
  form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
20
21
  <% end -%>
21
22
  end
23
+ <% else -%>
24
+ # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
25
+ assert_select "form", :action => <%= table_name %>_path, :method => "post" do
26
+ <% for attribute in output_attributes -%>
27
+ assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]"
28
+ <% end -%>
29
+ end
30
+ <% end -%>
22
31
  end
23
32
  end
@@ -15,7 +15,12 @@ describe "<%= table_name %>/show.html.<%= options[:template_engine] %>" do
15
15
  it "renders attributes in <p>" do
16
16
  render
17
17
  <% for attribute in output_attributes -%>
18
+ <% if webrat? -%>
18
19
  rendered.should contain(<%= value_for(attribute) %>.to_s)
20
+ <% else -%>
21
+ # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
22
+ rendered.should match(/<%= eval(value_for(attribute)) %>/)
23
+ <% end -%>
19
24
  <% end -%>
20
25
  end
21
26
  end
@@ -1,8 +1,10 @@
1
1
  module RSpec
2
2
  module Rails
3
3
  class Railtie < ::Rails::Railtie
4
- config.generators.integration_tool :rspec
5
- config.generators.test_framework :rspec
4
+ # Rails-3.0.1 requires config.app_generators instead of 3.0.0's config.generators
5
+ generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
6
+ generators.integration_tool :rspec
7
+ generators.test_framework :rspec
6
8
 
7
9
  rake_tasks do
8
10
  load "rspec/rails/tasks/rspec.rake"
@@ -169,6 +169,7 @@ module RSpec::Rails
169
169
  @routes = ::Rails.application.routes
170
170
  ActionController::Base.allow_forgery_protection = false
171
171
  end
172
+ subject { controller }
172
173
  end
173
174
 
174
175
  RSpec.configure &include_self_when_dir_matches('spec','controllers')
@@ -49,7 +49,7 @@ module RSpec::Rails
49
49
  # Returns an instance of ActionView::Base with the helper being specified
50
50
  # mixed in, along with any of the built-in rails helpers.
51
51
  def helper
52
- _view.tap {|v| v.assign(_assigns)}
52
+ _view.tap {|v| v.assign(view_assigns)}
53
53
  end
54
54
 
55
55
  private
@@ -21,5 +21,6 @@ end
21
21
  require 'rspec/rails/matchers/render_template'
22
22
  require 'rspec/rails/matchers/redirect_to'
23
23
  require 'rspec/rails/matchers/routing_matchers'
24
+ require 'rspec/rails/matchers/be_new_record'
24
25
  require 'rspec/rails/matchers/be_a_new'
25
26
  require 'rspec/rails/matchers/have_extension'
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :be_new_record do
2
+ match do |actual|
3
+ !actual.persisted?
4
+ end
5
+ end
@@ -5,27 +5,27 @@ module RSpec
5
5
 
6
6
  module Mocks
7
7
 
8
- module InstanceMethods
9
- def valid?
10
- true
11
- end
12
-
8
+ module ActiveModelInstanceMethods
13
9
  def as_new_record
10
+ self.stub(:persisted?) { false }
14
11
  self.stub(:id) { nil }
15
12
  self
16
13
  end
17
14
 
18
- def new_record?
19
- !persisted?
20
- end
21
-
22
15
  def persisted?
23
- !!id
16
+ true
24
17
  end
18
+ end
25
19
 
20
+ module ActiveRecordInstanceMethods
26
21
  def destroy
22
+ self.stub(:persisted?) { false }
27
23
  self.stub(:id) { nil }
28
24
  end
25
+
26
+ def new_record?
27
+ !persisted?
28
+ end
29
29
  end
30
30
 
31
31
  # Creates a mock object instance for a +string_or_model_class+ with
@@ -37,7 +37,7 @@ module RSpec
37
37
  # * A String representing a Class that does not exist
38
38
  # * A String representing a Class that extends ActiveModel::Naming
39
39
  # * A Class that extends ActiveModel::Naming
40
- def mock_model(string_or_model_class, options_and_stubs = {})
40
+ def mock_model(string_or_model_class, stubs = {})
41
41
  if String === string_or_model_class
42
42
  if Object.const_defined?(string_or_model_class)
43
43
  model_class = Object.const_get(string_or_model_class)
@@ -61,57 +61,71 @@ It received #{model_class.inspect}
61
61
  EOM
62
62
  end
63
63
 
64
- id = options_and_stubs.has_key?(:id) ? options_and_stubs[:id] : next_id
65
- options_and_stubs = options_and_stubs.reverse_merge({
66
- :id => id,
67
- :destroyed? => false,
68
- :marked_for_destruction? => false
69
- })
70
- derived_name = "#{model_class.name}_#{id}"
71
- m = mock(derived_name, options_and_stubs)
72
- m.extend InstanceMethods
73
- m.extend ActiveModel::Conversion
74
- errors = ActiveModel::Errors.new(m)
75
- [:save, :update_attributes].each do |key|
76
- if options_and_stubs[key] == false
77
- errors.stub(:empty?) { false }
64
+ stubs = stubs.reverse_merge(:id => next_id)
65
+ stubs = stubs.reverse_merge(:persisted? => !!stubs[:id])
66
+ stubs = stubs.reverse_merge(:destroyed? => false)
67
+ stubs = stubs.reverse_merge(:marked_for_destruction? => false)
68
+
69
+ mock("#{model_class.name}_#{stubs[:id]}", stubs).tap do |m|
70
+ m.extend ActiveModelInstanceMethods
71
+ m.singleton_class.__send__ :include, ActiveModel::Conversion
72
+ m.singleton_class.__send__ :include, ActiveModel::Validations
73
+ if RSpec::Rails::using_active_record?
74
+ m.extend ActiveRecordInstanceMethods
75
+ [:save, :update_attributes].each do |key|
76
+ if stubs[key] == false
77
+ m.errors.stub(:empty?) { false }
78
+ end
79
+ end
78
80
  end
81
+ m.__send__(:__mock_proxy).instance_eval(<<-CODE, __FILE__, __LINE__)
82
+ def @object.is_a?(other)
83
+ #{model_class}.ancestors.include?(other)
84
+ end
85
+ def @object.kind_of?(other)
86
+ #{model_class}.ancestors.include?(other)
87
+ end
88
+ def @object.instance_of?(other)
89
+ other == #{model_class}
90
+ end
91
+ def @object.respond_to?(method_name)
92
+ #{model_class}.respond_to?(:column_names) && #{model_class}.column_names.include?(method_name.to_s) || super
93
+ end
94
+ def @object.class
95
+ #{model_class}
96
+ end
97
+ def @object.to_s
98
+ "#{model_class.name}_#{to_param}"
99
+ end
100
+ CODE
101
+ yield m if block_given?
79
102
  end
80
- m.stub(:errors) { errors }
81
- m.__send__(:__mock_proxy).instance_eval(<<-CODE, __FILE__, __LINE__)
82
- def @object.is_a?(other)
83
- #{model_class}.ancestors.include?(other)
84
- end
85
- def @object.kind_of?(other)
86
- #{model_class}.ancestors.include?(other)
87
- end
88
- def @object.instance_of?(other)
89
- other == #{model_class}
90
- end
91
- def @object.respond_to?(method_name)
92
- #{model_class}.respond_to?(:column_names) && #{model_class}.column_names.include?(method_name.to_s) || super
93
- end
94
- def @object.class
95
- #{model_class}
96
- end
97
- def @object.to_s
98
- "#{model_class.name}_#{id}"
99
- end
100
- CODE
101
- yield m if block_given?
102
- m
103
103
  end
104
104
 
105
- module ModelStubber
106
- def connection
107
- raise RSpec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
105
+ module ActiveModelStubExtensions
106
+ def as_new_record
107
+ self.stub(:persisted?) { false }
108
+ self.stub(:id) { nil }
109
+ self
108
110
  end
109
- def new_record?
110
- __send__(self.class.primary_key).nil?
111
+
112
+ def persisted?
113
+ true
111
114
  end
115
+ end
116
+
117
+ module ActiveRecordStubExtensions
112
118
  def as_new_record
113
119
  self.__send__("#{self.class.primary_key}=", nil)
114
- self
120
+ super
121
+ end
122
+
123
+ def new_record?
124
+ !persisted?
125
+ end
126
+
127
+ def connection
128
+ raise RSpec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
115
129
  end
116
130
  end
117
131
 
@@ -121,12 +135,15 @@ EOM
121
135
  # stub_model(Model, hash_of_stubs)
122
136
  # stub_model(Model, instance_variable_name, hash_of_stubs)
123
137
  #
124
- # Creates an instance of +Model+ that is prohibited from accessing the
125
- # database*. For each key in +hash_of_stubs+, if the model has a
126
- # matching attribute (determined by asking it) are simply assigned the
127
- # submitted values. If the model does not have a matching attribute, the
128
- # key/value pair is assigned as a stub return value using RSpec's
129
- # mocking/stubbing framework.
138
+ # Creates an instance of +Model+ with +to_param+ stubbed using a
139
+ # generated value that is unique to each object.. If +Model+ is an
140
+ # +ActiveRecord+ model, it is prohibited from accessing the database*.
141
+ #
142
+ # For each key in +hash_of_stubs+, if the model has a matching attribute
143
+ # (determined by asking it) are simply assigned the submitted values. If
144
+ # the model does not have a matching attribute, the key/value pair is
145
+ # assigned as a stub return value using RSpec's mocking/stubbing
146
+ # framework.
130
147
  #
131
148
  # <tt>new_record?</tt> is overridden to return the result of id.nil?
132
149
  # This means that by default new_record? will return false. If you want
@@ -151,16 +168,25 @@ EOM
151
168
  #
152
169
  # stub_model(Person)
153
170
  # stub_model(Person).as_new_record
154
- # stub_model(Person, :id => 37)
171
+ # stub_model(Person, :to_param => 37)
155
172
  # stub_model(Person) do |person|
156
173
  # person.first_name = "David"
157
174
  # end
158
175
  def stub_model(model_class, stubs={})
159
- primary_key = model_class.primary_key.to_sym
160
- stubs = {primary_key => next_id}.merge(stubs)
161
176
  model_class.new.tap do |m|
162
- m.__send__("#{primary_key}=", stubs.delete(primary_key))
163
- m.extend ModelStubber
177
+ m.extend ActiveModelStubExtensions
178
+ if RSpec::Rails::using_active_record? && model_class < ActiveRecord::Base
179
+ m.extend ActiveRecordStubExtensions
180
+ primary_key = model_class.primary_key.to_sym
181
+ stubs = stubs.reverse_merge(primary_key => next_id)
182
+ stubs = stubs.reverse_merge(:persisted? => !!stubs[primary_key])
183
+ else
184
+ stubs = stubs.reverse_merge(:id => next_id)
185
+ stubs = stubs.reverse_merge(:persisted? => !!stubs[:id])
186
+ end
187
+ stubs.each do |k,v|
188
+ m.__send__("#{k}=", stubs.delete(k)) if m.respond_to?("#{k}=")
189
+ end
164
190
  m.stub(stubs)
165
191
  yield m if block_given?
166
192
  end
@@ -1,7 +1,7 @@
1
1
  module RSpec # :nodoc:
2
2
  module Rails # :nodoc:
3
3
  module Version # :nodoc:
4
- STRING = '2.0.0.beta.22'
4
+ STRING = '2.0.0.rc'
5
5
  end
6
6
  end
7
7
  end
@@ -13,15 +13,24 @@ module RSpec
13
13
  _encapsulated_assigns[key] = value
14
14
  end
15
15
 
16
+ if ::Rails::VERSION::STRING == "3.0.0"
17
+ def _assigns
18
+ super.merge(_encapsulated_assigns)
19
+ end
20
+ def view_assigns
21
+ _assigns
22
+ end
23
+ else # >= 3.0.1
24
+ def view_assigns
25
+ super.merge(_encapsulated_assigns)
26
+ end
27
+ end
28
+
16
29
  private
17
30
 
18
31
  def _encapsulated_assigns
19
32
  @_encapsulated_assigns ||= {}
20
33
  end
21
-
22
- def _assigns
23
- super.merge(_encapsulated_assigns)
24
- end
25
34
  end
26
35
  end
27
36
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
 
27
27
  Thank you for installing #{s.summary}!
28
28
 
29
- This version of rspec-rails only works with versions of rails >= 3.0.0.beta.4.
29
+ This version of rspec-rails only works with versions of rails >= 3.0.0
30
30
 
31
31
  To configure your app to use rspec-rails, add a declaration to your Gemfile.
32
32
  If you are using Bundler's grouping feature in your Gemfile, be sure to include
@@ -5,18 +5,35 @@ module RSpec::Rails
5
5
  it { should be_included_in_files_in('./spec/controllers/') }
6
6
  it { should be_included_in_files_in('.\\spec\\controllers\\') }
7
7
 
8
- it "includes routing matchers" do
9
- group = RSpec::Core::ExampleGroup.describe do
8
+ let(:group) do
9
+ RSpec::Core::ExampleGroup.describe do
10
10
  include ControllerExampleGroup
11
11
  end
12
+ end
13
+
14
+ it "includes routing matchers" do
12
15
  group.included_modules.should include(RSpec::Rails::Matchers::RoutingMatchers)
13
16
  end
14
17
 
15
18
  it "adds :type => :controller to the metadata" do
16
- group = RSpec::Core::ExampleGroup.describe do
17
- include ControllerExampleGroup
18
- end
19
19
  group.metadata[:type].should eq(:controller)
20
20
  end
21
+
22
+ context "with implicit subject" do
23
+ it "uses the controller as the subject" do
24
+ controller = double('controller')
25
+ example = group.new
26
+ example.stub(:controller => controller)
27
+ example.subject.should == controller
28
+ end
29
+ end
30
+
31
+ describe "with explicit subject" do
32
+ it "should use the specified subject instead of the controller" do
33
+ group.subject { 'explicit' }
34
+ example = group.new
35
+ example.subject.should == 'explicit'
36
+ end
37
+ end
21
38
  end
22
39
  end
@@ -24,7 +24,7 @@ module RSpec::Rails
24
24
  describe "#helper" do
25
25
  it "returns the instance of AV::Base provided by AV::TC::Behavior" do
26
26
  helper_spec = Object.new.extend HelperExampleGroup::InstanceMethods
27
- helper_spec.should_receive(:_assigns)
27
+ helper_spec.should_receive(:view_assigns)
28
28
  av_tc_b_view = double('_view')
29
29
  av_tc_b_view.should_receive(:assign)
30
30
  helper_spec.stub(:_view) { av_tc_b_view }
@@ -15,7 +15,7 @@ module RSpec::Rails
15
15
  describe 'automatic inclusion of helpers' do
16
16
  module ::ThingsHelper; end
17
17
 
18
- it 'includes the helper with the same name' do
18
+ pending 'includes the helper with the same name' do
19
19
  group = RSpec::Core::ExampleGroup.describe 'things/show.html.erb'
20
20
  group.should_receive(:helper).with(ThingsHelper)
21
21
  group.class_eval do
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe "be_new_record" do
4
+ context "un-persisted record" do
5
+ it "passes" do
6
+ record = double('record', :persisted? => false)
7
+ record.should be_new_record
8
+ end
9
+ end
10
+
11
+ context "persisted record" do
12
+ it "fails" do
13
+ record = double('record', :persisted? => true)
14
+ record.should_not be_new_record
15
+ end
16
+ end
17
+ end
@@ -14,6 +14,7 @@ end
14
14
 
15
15
  class NonActiveRecordModel
16
16
  extend ActiveModel::Naming
17
+ include ActiveModel::Conversion
17
18
  end
18
19
 
19
20
  class MockableModel < ActiveRecord::Base
@@ -41,19 +41,17 @@ describe "mock_model(RealModel)" do
41
41
  before(:each) do
42
42
  @model = mock_model(MockableModel, :id => 1)
43
43
  end
44
+
44
45
  it "is named using the stubbed id value" do
45
46
  @model.instance_variable_get(:@name).should == "MockableModel_1"
46
47
  end
47
- it "returns string of id value for to_param" do
48
- @model.to_param.should == "1"
49
- end
50
48
  end
51
49
 
52
50
  describe "destroy" do
53
- it "sets id to nil" do
51
+ it "sets persisted to false" do
54
52
  model = mock_model(MockableModel)
55
53
  model.destroy
56
- model.id.should be_nil
54
+ model.should_not be_persisted
57
55
  end
58
56
  end
59
57
 
@@ -181,26 +179,25 @@ describe "mock_model(RealModel)" do
181
179
  end
182
180
 
183
181
  describe "#persisted?" do
184
- context "with default id" do
182
+ context "with default identifier" do
185
183
  it "returns true" do
186
184
  mock_model(MockableModel).should be_persisted
187
185
  end
188
186
  end
189
187
 
190
- context "with explicit id" do
188
+ context "with explicit identifier via :id" do
191
189
  it "returns true" do
192
190
  mock_model(MockableModel, :id => 37).should be_persisted
193
191
  end
194
192
  end
195
193
 
196
- context "with id nil" do
194
+ context "with id => nil" do
197
195
  it "returns false" do
198
196
  mock_model(MockableModel, :id => nil).should_not be_persisted
199
197
  end
200
198
  end
201
199
  end
202
200
 
203
-
204
201
  describe "#valid?" do
205
202
  context "default" do
206
203
  it "returns true" do
@@ -220,6 +217,11 @@ describe "mock_model(RealModel)" do
220
217
  m.as_new_record.should be_new_record
221
218
  end
222
219
 
220
+ it "says it is not persisted" do
221
+ m = mock_model(MockableModel)
222
+ m.as_new_record.should_not be_persisted
223
+ end
224
+
223
225
  it "has a nil id" do
224
226
  mock_model(MockableModel).as_new_record.id.should be(nil)
225
227
  end
@@ -2,100 +2,149 @@ require 'spec_helper'
2
2
  require File.dirname(__FILE__) + '/ar_classes'
3
3
 
4
4
  describe "stub_model" do
5
- describe "defaults" do
6
- it "has an id" do
7
- stub_model(MockableModel).id.should be > 0
5
+
6
+ shared_examples_for "stub model" do
7
+ describe "with a block" do
8
+ it "yields the model" do
9
+ model = stub_model(model_class) do |block_arg|
10
+ @block_arg = block_arg
11
+ end
12
+ model.should be(@block_arg)
13
+ end
8
14
  end
9
15
 
10
- it "says it is not a new record" do
11
- stub_model(MockableModel).should_not be_new_record
16
+ describe "#persisted?" do
17
+ context "default" do
18
+ it "returns true" do
19
+ model = stub_model(model_class)
20
+ model.should be_persisted
21
+ end
22
+ end
23
+
24
+ context "with as_new_record" do
25
+ it "returns false" do
26
+ model = stub_model(model_class).as_new_record
27
+ model.should_not be_persisted
28
+ end
29
+ end
12
30
  end
13
- end
14
31
 
15
- it "accepts a stub id" do
16
- stub_model(MockableModel, :id => 37).id.should == 37
17
- end
32
+ it "increments the value returned by to_param" do
33
+ first = stub_model(model_class)
34
+ second = stub_model(model_class)
35
+ second.to_param.to_i.should == (first.to_param.to_i + 1)
36
+ end
18
37
 
19
- it "says it is a new record when id is set to nil" do
20
- stub_model(MockableModel, :id => nil).should be_new_record
21
38
  end
22
39
 
23
- it "accepts any arbitrary stub" do
24
- stub_model(MockableModel, :foo => "bar").foo.should == "bar"
40
+ context "with ActiveModel (not ActiveRecord)" do
41
+ it_behaves_like "stub model" do
42
+ def model_class
43
+ NonActiveRecordModel
44
+ end
45
+ end
25
46
  end
26
47
 
27
- it "accepts a stub for save" do
28
- stub_model(MockableModel, :save => false).save.should be(false)
29
- end
30
-
31
- describe "alternate primary key" do
32
- it "has the correct primary_key name" do
33
- stub_model(AlternatePrimaryKeyModel).class.primary_key.should eql('my_id')
48
+ context "with an ActiveRecord model" do
49
+ let(:model_class) { MockableModel }
50
+
51
+ it_behaves_like "stub model"
52
+
53
+ describe "#new_record?" do
54
+ context "default" do
55
+ it "returns false" do
56
+ model = stub_model(model_class)
57
+ model.new_record?.should be_false
58
+ end
59
+ end
60
+
61
+ context "with as_new_record" do
62
+ it "returns true" do
63
+ model = stub_model(model_class).as_new_record
64
+ model.new_record?.should be_true
65
+ end
66
+ end
34
67
  end
35
-
36
- it "has a primary_key" do
37
- stub_model(AlternatePrimaryKeyModel).my_id.should be > 0
68
+
69
+ describe "defaults" do
70
+ it "has an id" do
71
+ stub_model(MockableModel).id.should be > 0
72
+ end
73
+
74
+ it "says it is not a new record" do
75
+ stub_model(MockableModel).should_not be_new_record
76
+ end
38
77
  end
39
-
40
- it "does not say it is a new record" do
41
- stub_model(AlternatePrimaryKeyModel).should_not be_new_record
78
+
79
+ describe "#as_new_record" do
80
+ it "has a nil id" do
81
+ stub_model(MockableModel).as_new_record.id.should be(nil)
82
+ end
42
83
  end
43
-
44
- it "says it is a new record if primary_key is nil" do
45
- stub_model(AlternatePrimaryKeyModel, :my_id => nil).should be_new_record
84
+
85
+ it "raises when hitting the db" do
86
+ lambda do
87
+ stub_model(ConnectableModel).connection
88
+ end.should raise_error(RSpec::Rails::IllegalDataAccessException, /stubbed models are not allowed to access the database/)
46
89
  end
47
-
48
- it "accepts a stub for the primary_key" do
49
- stub_model(AlternatePrimaryKeyModel, :my_id => 5).my_id.should == 5
90
+
91
+ it "increments the id" do
92
+ first = stub_model(model_class)
93
+ second = stub_model(model_class)
94
+ second.id.should == (first.id + 1)
50
95
  end
51
- end
52
96
 
53
- describe "#as_new_record" do
54
- it "says it is a new record" do
55
- stub_model(MockableModel).as_new_record.should be_new_record
97
+ it "accepts a stub id" do
98
+ stub_model(MockableModel, :id => 37).id.should == 37
56
99
  end
57
100
 
58
- it "has a nil id" do
59
- stub_model(MockableModel).as_new_record.id.should be(nil)
101
+ it "says it is a new record when id is set to nil" do
102
+ stub_model(MockableModel, :id => nil).should be_new_record
60
103
  end
61
- end
62
104
 
63
- it "raises when hitting the db" do
64
- lambda do
65
- stub_model(ConnectableModel).connection
66
- end.should raise_error(RSpec::Rails::IllegalDataAccessException, /stubbed models are not allowed to access the database/)
67
- end
105
+ it "accepts a stub for save" do
106
+ stub_model(MockableModel, :save => false).save.should be(false)
107
+ end
68
108
 
69
- it "increments the id" do
70
- first = stub_model(MockableModel)
71
- second = stub_model(MockableModel)
72
- second.id.should == (first.id + 1)
73
- end
109
+ describe "alternate primary key" do
110
+ it "has the correct primary_key name" do
111
+ stub_model(AlternatePrimaryKeyModel).class.primary_key.should eql('my_id')
112
+ end
74
113
 
75
- describe "as association" do
76
- before(:each) do
77
- @real = AssociatedModel.create!
78
- @stub_model = stub_model(MockableModel)
79
- @real.mockable_model = @stub_model
80
- end
114
+ it "has a primary_key" do
115
+ stub_model(AlternatePrimaryKeyModel).my_id.should be > 0
116
+ end
81
117
 
82
- it "passes associated_model == mock" do
83
- @stub_model.should == @real.mockable_model
84
- end
118
+ it "says it is not a new record" do
119
+ stub_model(AlternatePrimaryKeyModel) do |m|
120
+ m.should_not be_new_record
121
+ end
122
+ end
85
123
 
86
- it "passes mock == associated_model" do
87
- @real.mockable_model.should == @stub_model
124
+ it "says it is a new record if primary_key is nil" do
125
+ stub_model(AlternatePrimaryKeyModel, :my_id => nil).should be_new_record
126
+ end
127
+
128
+ it "accepts a stub for the primary_key" do
129
+ stub_model(AlternatePrimaryKeyModel, :my_id => 5).my_id.should == 5
130
+ end
88
131
  end
89
- end
90
132
 
91
- describe "with a block" do
92
- it "yields the model" do
93
- model = stub_model(MockableModel) do |block_arg|
94
- @block_arg = block_arg
133
+ describe "as association" do
134
+ before(:each) do
135
+ @real = AssociatedModel.create!
136
+ @stub_model = stub_model(MockableModel)
137
+ @real.mockable_model = @stub_model
138
+ end
139
+
140
+ it "passes associated_model == mock" do
141
+ @stub_model.should == @real.mockable_model
142
+ end
143
+
144
+ it "passes mock == associated_model" do
145
+ @real.mockable_model.should == @stub_model
95
146
  end
96
- model.should be(@block_arg)
97
147
  end
148
+
98
149
  end
99
150
  end
100
-
101
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-rails
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 7712058
4
5
  prerelease: true
5
6
  segments:
6
7
  - 2
7
8
  - 0
8
9
  - 0
9
- - beta
10
- - 22
11
- version: 2.0.0.beta.22
10
+ - rc
11
+ version: 2.0.0.rc
12
12
  platform: ruby
13
13
  authors:
14
14
  - David Chelimsky
@@ -17,26 +17,26 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-09-12 00:00:00 -05:00
20
+ date: 2010-10-04 00:00:00 -05:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
24
- name: rspec
25
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
25
  none: false
27
26
  requirements:
28
27
  - - "="
29
28
  - !ruby/object:Gem::Version
29
+ hash: 7712058
30
30
  segments:
31
31
  - 2
32
32
  - 0
33
33
  - 0
34
- - beta
35
- - 22
36
- version: 2.0.0.beta.22
34
+ - rc
35
+ version: 2.0.0.rc
36
+ requirement: *id001
37
37
  type: :runtime
38
+ name: rspec
38
39
  prerelease: false
39
- version_requirements: *id001
40
40
  description: RSpec-2 for Rails-3
41
41
  email: dchelimsky@gmail.com;chad.humphries@gmail.com
42
42
  executables: []
@@ -53,6 +53,7 @@ files:
53
53
  - History.md
54
54
  - README.markdown
55
55
  - Rakefile
56
+ - Thorfile
56
57
  - Upgrade.markdown
57
58
  - autotest/discover.rb
58
59
  - cucumber.yml
@@ -124,6 +125,7 @@ files:
124
125
  - lib/rspec/rails/fixture_support.rb
125
126
  - lib/rspec/rails/matchers.rb
126
127
  - lib/rspec/rails/matchers/be_a_new.rb
128
+ - lib/rspec/rails/matchers/be_new_record.rb
127
129
  - lib/rspec/rails/matchers/have_extension.rb
128
130
  - lib/rspec/rails/matchers/redirect_to.rb
129
131
  - lib/rspec/rails/matchers/render_template.rb
@@ -147,6 +149,7 @@ files:
147
149
  - spec/rspec/rails/example/view_rendering_spec.rb
148
150
  - spec/rspec/rails/fixture_support_spec.rb
149
151
  - spec/rspec/rails/matchers/be_a_new_spec.rb
152
+ - spec/rspec/rails/matchers/be_new_record_spec.rb
150
153
  - spec/rspec/rails/matchers/errors_on_spec.rb
151
154
  - spec/rspec/rails/matchers/redirect_to_spec.rb
152
155
  - spec/rspec/rails/matchers/render_template_spec.rb
@@ -167,9 +170,9 @@ licenses: []
167
170
  post_install_message: |
168
171
  **************************************************
169
172
 
170
- Thank you for installing rspec-rails-2.0.0.beta.22!
173
+ Thank you for installing rspec-rails-2.0.0.rc!
171
174
 
172
- This version of rspec-rails only works with versions of rails >= 3.0.0.beta.4.
175
+ This version of rspec-rails only works with versions of rails >= 3.0.0
173
176
 
174
177
  To configure your app to use rspec-rails, add a declaration to your Gemfile.
175
178
  If you are using Bundler's grouping feature in your Gemfile, be sure to include
@@ -177,7 +180,7 @@ post_install_message: |
177
180
  can access its generators and rake tasks.
178
181
 
179
182
  group :development, :test do
180
- gem "rspec-rails", ">= 2.0.0.beta.22"
183
+ gem "rspec-rails", ">= 2.0.0.rc"
181
184
  end
182
185
 
183
186
  Be sure to run the following command in each of your Rails apps if you're
@@ -208,7 +211,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
208
211
  requirements:
209
212
  - - ">="
210
213
  - !ruby/object:Gem::Version
211
- hash: 4218695540923363031
214
+ hash: 3
212
215
  segments:
213
216
  - 0
214
217
  version: "0"
@@ -217,6 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
220
  requirements:
218
221
  - - ">"
219
222
  - !ruby/object:Gem::Version
223
+ hash: 25
220
224
  segments:
221
225
  - 1
222
226
  - 3
@@ -228,7 +232,7 @@ rubyforge_project: rspec
228
232
  rubygems_version: 1.3.7
229
233
  signing_key:
230
234
  specification_version: 3
231
- summary: rspec-rails-2.0.0.beta.22
235
+ summary: rspec-rails-2.0.0.rc
232
236
  test_files:
233
237
  - features/README.markdown
234
238
  - features/controller_specs/anonymous_controller.feature
@@ -262,6 +266,7 @@ test_files:
262
266
  - spec/rspec/rails/example/view_rendering_spec.rb
263
267
  - spec/rspec/rails/fixture_support_spec.rb
264
268
  - spec/rspec/rails/matchers/be_a_new_spec.rb
269
+ - spec/rspec/rails/matchers/be_new_record_spec.rb
265
270
  - spec/rspec/rails/matchers/errors_on_spec.rb
266
271
  - spec/rspec/rails/matchers/redirect_to_spec.rb
267
272
  - spec/rspec/rails/matchers/render_template_spec.rb