diff options
Diffstat (limited to 'vendor/plugins/rspec_on_rails/lib')
19 files changed, 361 insertions, 146 deletions
diff --git a/vendor/plugins/rspec_on_rails/lib/autotest/discover.rb b/vendor/plugins/rspec_on_rails/lib/autotest/discover.rb new file mode 100644 index 000000000..8e6968e20 --- /dev/null +++ b/vendor/plugins/rspec_on_rails/lib/autotest/discover.rb @@ -0,0 +1 @@ +# This needs to be here for >= ZenTest-3.9.0 to add this directory to the load path.
\ No newline at end of file diff --git a/vendor/plugins/rspec_on_rails/lib/autotest/rails_rspec.rb b/vendor/plugins/rspec_on_rails/lib/autotest/rails_rspec.rb index 1fe20fbc9..c6fe446b1 100644 --- a/vendor/plugins/rspec_on_rails/lib/autotest/rails_rspec.rb +++ b/vendor/plugins/rspec_on_rails/lib/autotest/rails_rspec.rb @@ -26,7 +26,7 @@ require 'active_support' require 'autotest/rspec' Autotest.add_hook :initialize do |at| - %w{config coverage db doc log public script vendor/rails vendor/plugins previous_failures.txt}.each do |exception| + %w{config/ coverage/ db/ doc/ log/ public/ script/ tmp/ vendor/rails vendor/plugins previous_failures.txt}.each do |exception| at.add_exception(exception) end diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails.rb index be8a6c415..74fc3929b 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails.rb @@ -8,10 +8,11 @@ require 'test/unit' require 'spec' -require 'spec/rails/extensions' +require 'spec/rails/matchers' +require 'spec/rails/mocks' require 'spec/rails/example' +require 'spec/rails/extensions' require 'spec/rails/version' -require 'spec/rails/matchers' module Spec # = Spec::Rails diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/example.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/example.rb index 64a72a9db..f104f51e5 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/example.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/example.rb @@ -1,6 +1,5 @@ dir = File.dirname(__FILE__) -require 'spec/rails/example/ivar_proxy' require 'spec/rails/example/assigns_hash_proxy' require "spec/rails/example/render_observer" diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/assigns_hash_proxy.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/assigns_hash_proxy.rb index 1d121f70a..c8a7d0662 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/assigns_hash_proxy.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/assigns_hash_proxy.rb @@ -17,6 +17,7 @@ module Spec end def []=(ivar, val) + @object.instance_variable_set "@#{ivar}", val assigns[ivar.to_s] = val end diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/controller_example_group.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/controller_example_group.rb index eca06a403..a686b6a39 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/controller_example_group.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/controller_example_group.rb @@ -72,12 +72,19 @@ module Spec # # See Spec::Rails::Example::ControllerExampleGroup for more information about # Integration and Isolation modes. - def integrate_views - @integrate_views = true + def integrate_views(integrate_views = true) + @integrate_views = integrate_views end + def integrate_views? # :nodoc: @integrate_views end + + def inherited(klass) # :nodoc: + klass.controller_class_name = controller_class_name + klass.integrate_views(integrate_views?) + super + end # You MUST provide a controller_name within the context of # your controller specs: @@ -106,7 +113,7 @@ module Spec end EOE end - @controller.metaclass.class_eval do + (class << @controller; self; end).class_eval do def controller_path #:nodoc: self.class.name.underscore.gsub('_controller', '') end @@ -150,8 +157,8 @@ module Spec end protected - def _controller_ivar_proxy - @controller_ivar_proxy ||= AssignsHashProxy.new @controller + def _assigns_hash_proxy + @_assigns_hash_proxy ||= AssignsHashProxy.new @controller end private @@ -162,18 +169,32 @@ module Spec module ControllerInstanceMethods #:nodoc: include Spec::Rails::Example::RenderObserver - # === render(options = nil, deprecated_status = nil, &block) + # === render(options = nil, deprecated_status_or_extra_options = nil, &block) # # This gets added to the controller's singleton meta class, # allowing Controller Examples to run in two modes, freely switching # from context to context. - def render(options=nil, deprecated_status=nil, &block) + def render(options=nil, deprecated_status_or_extra_options=nil, &block) + if ::Rails::VERSION::STRING >= '2.0.0' && deprecated_status_or_extra_options.nil? + deprecated_status_or_extra_options = {} + end + unless block_given? unless integrate_views? - @template.metaclass.class_eval do - define_method :file_exists? do - true + if @template.respond_to?(:finder) + (class << @template.finder; self; end).class_eval do + define_method :file_exists? do + true + end + end + else + (class << @template; self; end).class_eval do + define_method :file_exists? do + true + end end + end + (class << @template; self; end).class_eval do define_method :render_file do |*args| @first_render ||= args[0] end @@ -181,43 +202,39 @@ module Spec end end - if expect_render_mock_proxy.send(:__mock_proxy).send(:find_matching_expectation, :render, options) - expect_render_mock_proxy.render(options) + if matching_message_expectation_exists(options) + expect_render_mock_proxy.render(options, &block) @performed_render = true else - unless expect_render_mock_proxy.send(:__mock_proxy).send(:find_matching_method_stub, :render, options) - super(options, deprecated_status, &block) + unless matching_stub_exists(options) + super(options, deprecated_status_or_extra_options, &block) end end end + + def raise_with_disable_message(old_method, new_method) + raise %Q| + controller.#{old_method}(:render) has been disabled because it + can often produce unexpected results. Instead, you should + use the following (before the action): - if self.respond_to?(:should_receive) && self.respond_to?(:stub!) - self.send :alias_method, :orig_should_receive, :should_receive - self.send :alias_method, :orig_stub!, :stub! - def raise_with_disable_message(old_method, new_method) - raise %Q| - controller.#{old_method}(:render) has been disabled because it - can often produce unexpected results. Instead, you should - use the following (before the action): - - controller.#{new_method}(*args) + controller.#{new_method}(*args) - See the rdoc for #{new_method} for more information. - | - end - def should_receive(*args) - if args[0] == :render - raise_with_disable_message("should_receive", "expect_render") - else - orig_should_receive(*args) - end + See the rdoc for #{new_method} for more information. + | + end + def should_receive(*args) + if args[0] == :render + raise_with_disable_message("should_receive", "expect_render") + else + super end - def stub!(*args) - if args[0] == :render - raise_with_disable_message("stub!", "stub_render") - else - orig_stub!(*args) - end + end + def stub!(*args) + if args[0] == :render + raise_with_disable_message("stub!", "stub_render") + else + super end end @@ -236,6 +253,15 @@ module Spec def integrate_views? @integrate_views end + + def matching_message_expectation_exists(options) + expect_render_mock_proxy.send(:__mock_proxy).send(:find_matching_expectation, :render, options) + end + + def matching_stub_exists(options) + expect_render_mock_proxy.send(:__mock_proxy).send(:find_matching_method_stub, :render, options) + end + end Spec::Example::ExampleGroupFactory.register(:controller, self) diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/functional_example_group.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/functional_example_group.rb index 6f5790cbf..6bdb1823a 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/functional_example_group.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/functional_example_group.rb @@ -11,9 +11,9 @@ module Spec raise "Can't determine controller class for #{@controller_class_name}" if @controller_class.nil? @controller = @controller_class.new - @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + @response.session = @request.session end def params @@ -25,14 +25,14 @@ module Spec end def session - request.session + @response.session end # :call-seq: # assigns() # - # Hash of instance variables to values that are made available to views. - # == Examples + # Hash of instance variables to values that are made available to + # views. == Examples # # #in thing_controller.rb # def new @@ -43,23 +43,16 @@ module Spec # get 'new' # assigns[:registration].should == Thing.new #-- - # NOTE - Even though docs say only use assigns[:key] format, but allowing assigns(:key) - # in order to avoid breaking old specs. + # NOTE - Even though docs only use assigns[:key] format, this supports + # assigns(:key) in order to avoid breaking old specs. #++ def assigns(key = nil) if key.nil? - @controller.assigns - _controller_ivar_proxy + _assigns_hash_proxy else - @controller.assigns[key] - _controller_ivar_proxy[key] + _assigns_hash_proxy[key] end end - - protected - def _controller_ivar_proxy - @controller_ivar_proxy ||= IvarProxy.new @controller - end end end end diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/helper_example_group.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/helper_example_group.rb index 10c1ab002..7e60728ef 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/helper_example_group.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/helper_example_group.rb @@ -26,15 +26,56 @@ module Spec # end # end class HelperExampleGroup < FunctionalExampleGroup + class HelperObject < ActionView::Base + def protect_against_forgery? + false + end + end + class << self # The helper name.... def helper_name(name=nil) - send :include, "#{name}_helper".camelize.constantize + @helper_being_described = "#{name}_helper".camelize.constantize + send :include, @helper_being_described + end + + def helper + @helper_object ||= returning HelperObject.new do |helper_object| + if @helper_being_described.nil? + if described_type.class == Module + helper_object.extend described_type + end + else + helper_object.extend @helper_being_described + end + end end end + + # Returns an instance of ActionView::Base with the helper being spec'd + # included. + # + # == Example + # + # describe PersonHelper do + # it "should write a link to person with the name" do + # assigns[:person] = mock_model(Person, :full_name => "Full Name", :id => 37, :new_record? => false) + # helper.link_to_person.should == %{<a href="/people/37">Full Name</a>} + # end + # end + # + # module PersonHelper + # def link_to_person + # link_to person.full_name, url_for(person) + # end + # end + # + def helper + self.class.helper + end - # Reverse the load order so that custom helpers which - # are defined last are also loaded last. + # Reverse the load order so that custom helpers which are defined last + # are also loaded last. ActionView::Base.included_modules.reverse.each do |mod| include mod if mod.parents.include?(ActionView::Helpers) end @@ -58,10 +99,11 @@ module Spec end def eval_erb(text) - ERB.new(text).result(binding) + helper.instance_eval do + ERB.new(text).result(binding) + end end - # TODO: BT - Helper Examples should proxy method_missing to a Rails View instance. # When that is done, remove this method def protect_against_forgery? @@ -69,6 +111,12 @@ module Spec end Spec::Example::ExampleGroupFactory.register(:helper, self) + + protected + def _assigns_hash_proxy + @_assigns_hash_proxy ||= AssignsHashProxy.new helper + end + end class HelperBehaviourController < ApplicationController #:nodoc: diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/rails_example_group.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/rails_example_group.rb index a3df05ab5..444740d75 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/rails_example_group.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/rails_example_group.rb @@ -4,8 +4,10 @@ ActionView::Base.cache_template_extensions = false module Spec module Rails + module Example class RailsExampleGroup < Test::Unit::TestCase + # Rails >= r8570 uses setup/teardown_fixtures explicitly before(:each) do setup_fixtures if self.respond_to?(:setup_fixtures) @@ -15,53 +17,10 @@ module Spec end include Spec::Rails::Matchers - - @@model_id = 1000 - # Creates a mock object instance for a +model_class+ with common - # methods stubbed out. - # Additional methods may be easily stubbed (via add_stubs) if +stubs+ is passed. - def mock_model(model_class, options_and_stubs = {}) - # null = options_and_stubs.delete(:null_object) - # stubs = options_and_stubs - id = @@model_id - @@model_id += 1 - options_and_stubs = { - :id => id, - :to_param => id.to_s, - :new_record? => false, - :errors => stub("errors", :count => 0) - }.merge(options_and_stubs) - m = mock("#{model_class.name}_#{id}", options_and_stubs) - m.send(:__mock_proxy).instance_eval <<-CODE - def @target.is_a?(other) - #{model_class}.ancestors.include?(other) - end - def @target.kind_of?(other) - #{model_class}.ancestors.include?(other) - end - def @target.instance_of?(other) - other == #{model_class} - end - def @target.class - #{model_class} - end - CODE - yield m if block_given? - m - end - - #-- - # TODO - Shouldn't this just be an extension of stub! ?? - # - object.stub!(:method => return_value, :method2 => return_value2, :etc => etc) - #++ - # Stubs methods on +object+ (if +object+ is a symbol or string a new mock - # with that name will be created). +stubs+ is a Hash of <tt>method=>value</tt> - def add_stubs(object, stubs = {}) #:nodoc: - m = [String, Symbol].index(object.class) ? mock(object.to_s) : object - stubs.each {|k,v| m.stub!(k).and_return(v)} - m - end + include Spec::Rails::Mocks + Spec::Example::ExampleGroupFactory.default(self) + end end end diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/render_observer.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/render_observer.rb index 285e8b657..31086e227 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/render_observer.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/render_observer.rb @@ -1,4 +1,4 @@ -require 'spec/mocks' +require 'spec/mocks/framework' module Spec module Rails diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/view_example_group.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/view_example_group.rb index d7b567448..e77d2fbd8 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/example/view_example_group.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/example/view_example_group.rb @@ -28,6 +28,10 @@ module Spec ensure_that_flash_and_session_work_properly end + after(:each) do + ensure_that_base_view_path_is_not_set_across_example_groups + end + def initialize(defined_description, &implementation) #:nodoc: super @controller_class_name = "Spec::Rails::Example::ViewExampleGroupController" @@ -40,11 +44,6 @@ module Spec @controller.class.send :public, :flash end - def teardown #:nodoc: - super - ensure_that_base_view_path_is_not_set_across_example_groups - end - def ensure_that_base_view_path_is_not_set_across_example_groups #:nodoc: ActionView::Base.base_view_path = nil end @@ -73,7 +72,7 @@ module Spec return options[render_type] end end - raise Exception.new("Unhandled render type in view spec.") + return "" end def add_helpers(options) #:nodoc: @@ -84,7 +83,8 @@ module Spec end # Renders a template for a View Spec, which then provides access to the result - # through the +response+. + # through the +response+. Also supports render with :inline, which you can + # use to spec custom form builders, helpers, etc, in the context of a view. # # == Examples # @@ -92,6 +92,7 @@ module Spec # render('/people/list', :helper => MyHelper) # render('/people/list', :helpers => [MyHelper, MyOtherHelper]) # render(:partial => '/people/_address') + # render(:inline => "<% custom_helper 'argument', 'another argument' %>") # # See Spec::Rails::Example::ViewExampleGroup for more information. def render(*args) @@ -111,7 +112,7 @@ module Spec defaults = { :layout => false } options = defaults.merge options - @controller.instance_variable_set :@params, @request.parameters + @controller.send(:params).reverse_merge! @request.parameters @controller.send :initialize_current_url @@ -146,6 +147,11 @@ module Spec end Spec::Example::ExampleGroupFactory.register(:view, self) + + protected + def _assigns_hash_proxy + @_assigns_hash_proxy ||= AssignsHashProxy.new @controller + end end class ViewExampleGroupController < ApplicationController #:nodoc: @@ -162,7 +168,7 @@ module Spec rescue return end - template.metaclass.class_eval do + (class << template; self; end).class_eval do include helper_module end end diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/extensions/spec/example/configuration.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/extensions/spec/example/configuration.rb index 06e322637..22d40a08b 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/extensions/spec/example/configuration.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/extensions/spec/example/configuration.rb @@ -1,5 +1,5 @@ require 'spec/example/configuration' - +begin module Spec module Example class Configuration @@ -64,3 +64,8 @@ module Spec end end end +rescue Exception => e + puts e.message + puts e.backtrace + raise e +end
\ No newline at end of file diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers.rb index 27ac2ba0f..6c18b2a99 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers.rb @@ -1,6 +1,7 @@ dir = File.dirname(__FILE__) require 'spec/rails/matchers/assert_select' require 'spec/rails/matchers/have_text' +require 'spec/rails/matchers/include_text' require 'spec/rails/matchers/redirect_to' require 'spec/rails/matchers/render_template' diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/have_text.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/have_text.rb index 12c71ead8..e03029c7f 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/have_text.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/have_text.rb @@ -39,9 +39,11 @@ module Spec # Accepts a String or a Regexp, matching a String using == # and a Regexp using =~. # + # If response_or_text has a #body, then that is used as to match against + # else it uses response_or_text + # # Use this instead of <tt>response.should have_tag()</tt> - # when you either don't know or don't care where on the page - # this text appears. + # when you want to match the whole string or whole body # # == Examples # diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/include_text.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/include_text.rb new file mode 100644 index 000000000..4be25bce6 --- /dev/null +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/include_text.rb @@ -0,0 +1,54 @@ +module Spec + module Rails + module Matchers + + class IncludeText #:nodoc: + + def initialize(expected) + @expected = expected + end + + def matches?(response_or_text) + @actual = response_or_text.respond_to?(:body) ? response_or_text.body : response_or_text + return actual.include?(expected) + end + + def failure_message + "expected to find #{expected.inspect} in #{actual.inspect}" + end + + def negative_failure_message + "expected not to include text #{expected.inspect}" + end + + def to_s + "include text #{expected.inspect}" + end + + private + attr_reader :expected + attr_reader :actual + + end + + + # :call-seq: + # response.should include_text(expected) + # response.should_not include_text(expected) + # + # Accepts a String, matching using include? + # + # Use this instead of <tt>response.should have_text()</tt> + # when you either don't know or don't care where on the page + # this text appears. + # + # == Examples + # + # response.should include_text("This text will be in the actual string") + def include_text(text) + IncludeText.new(text) + end + + end + end +end
\ No newline at end of file diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb index 7c61c81e7..12ce92516 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb @@ -39,7 +39,7 @@ module Spec end def path_hash(url) - path = url.sub(%r{^\w+://#{@request.host}}, "").split("?", 2)[0] + path = url.sub(%r{^\w+://#{@request.host}(?::\d+)?}, "").split("?", 2)[0] ActionController::Routing::Routes.recognize_path path end diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/render_template.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/render_template.rb index 8c3df3ad2..e36c8bce0 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/render_template.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/render_template.rb @@ -18,6 +18,10 @@ module Spec "expected #{@expected.inspect}, got #{@actual.inspect}" end + def negative_failure_message + "expected not to render #{@expected.inspect}, but did" + end + def description "render template #{@expected.inspect}" end diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/mocks.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/mocks.rb new file mode 100644 index 000000000..34e1d18f4 --- /dev/null +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/mocks.rb @@ -0,0 +1,115 @@ +module Spec + module Rails + + class IllegalDataAccessException < StandardError; end + + module Mocks + + # Creates a mock object instance for a +model_class+ with common + # methods stubbed out. Additional methods may be easily stubbed (via + # add_stubs) if +stubs+ is passed. + def mock_model(model_class, options_and_stubs = {}) + id = next_id + options_and_stubs.reverse_merge!({ + :id => id, + :to_param => id.to_s, + :new_record? => false, + :errors => stub("errors", :count => 0) + }) + m = mock("#{model_class.name}_#{options_and_stubs[:id]}", options_and_stubs) + m.send(:__mock_proxy).instance_eval <<-CODE + def @target.is_a?(other) + #{model_class}.ancestors.include?(other) + end + def @target.kind_of?(other) + #{model_class}.ancestors.include?(other) + end + def @target.instance_of?(other) + other == #{model_class} + end + def @target.class + #{model_class} + end + CODE + yield m if block_given? + m + end + + # :call-seq: + # stub_model(Model) + # stub_model(Model).as_new_record + # stub_model(Model, hash_of_stubs) + # + # Creates an instance of +Model+ that is prohibited from accessing the + # database. For each key in +hash_of_stubs+, if the model has a + # matching attribute (determined by asking it, which it answers based + # on schema.rb) are simply assigned the submitted values. If the model + # does not have a matching attribute, the key/value pair is assigned + # as a stub return value using RSpec's mocking/stubbing framework. + # + # new_record? is overridden to return the result of id.nil? This means + # that by default new_record? will return false. If you want the + # object to behave as a new record, sending it +as_new_record+ will + # set the id to nil. You can also explicitly set :id => nil, in which + # case new_record? will return true, but using +as_new_record+ makes + # the example a bit more descriptive. + # + # While you can use stub_model in any example (model, view, + # controller, helper), it is especially useful in view examples, + # which are inherently more state-based than interaction-based. + # + # == Examples + # + # stub_model(Person) + # stub_model(Person).as_new_record + # stub_model(Person, :id => 37) + # stub_model(Person) do |person| + # model.first_name = "David" + # end + def stub_model(model_class, stubs = {}) + stubs = {:id => next_id}.merge(stubs) + returning model_class.new do |model| + model.id = stubs.delete(:id) + (class << model; self; end).class_eval do + def connection + raise Spec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database") + end + def new_record? + id.nil? + end + def as_new_record + self.id = nil + self + end + end + stubs.each do |k,v| + if model.has_attribute?(k) + model[k] = stubs.delete(k) + end + end + add_stubs(model, stubs) + yield model if block_given? + end + end + + #-- + # TODO - Shouldn't this just be an extension of stub! ?? + # - object.stub!(:method => return_value, :method2 => return_value2, :etc => etc) + #++ + # Stubs methods on +object+ (if +object+ is a symbol or string a new mock + # with that name will be created). +stubs+ is a Hash of <tt>method=>value</tt> + def add_stubs(object, stubs = {}) #:nodoc: + m = [String, Symbol].index(object.class) ? mock(object.to_s) : object + stubs.each {|k,v| m.stub!(k).and_return(v)} + m + end + + private + @@model_id = 1000 + def next_id + @@model_id += 1 + end + + end + end +end
\ No newline at end of file diff --git a/vendor/plugins/rspec_on_rails/lib/spec/rails/version.rb b/vendor/plugins/rspec_on_rails/lib/spec/rails/version.rb index 47043a07e..68591125e 100644 --- a/vendor/plugins/rspec_on_rails/lib/spec/rails/version.rb +++ b/vendor/plugins/rspec_on_rails/lib/spec/rails/version.rb @@ -1,23 +1,23 @@ -module Spec - module Rails - module VERSION #:nodoc: - BUILD_TIME_UTC = 20080114022430 - end - end -end - -# Verify that the plugin has the same revision as RSpec -if Spec::Rails::VERSION::BUILD_TIME_UTC != Spec::VERSION::BUILD_TIME_UTC - raise <<-EOF - -############################################################################ -Your RSpec on Rails plugin is incompatible with your installed RSpec. - -RSpec : #{Spec::VERSION::BUILD_TIME_UTC} -RSpec on Rails : #{Spec::Rails::VERSION::BUILD_TIME_UTC} - -Make sure your RSpec on Rails plugin is compatible with your RSpec gem. -See http://rspec.rubyforge.org/documentation/rails/install.html for details. -############################################################################ -EOF -end +module Spec
+ module Rails
+ module VERSION #:nodoc:
+ BUILD_TIME_UTC = 20080526202855
+ end
+ end
+end
+
+# Verify that the plugin has the same revision as RSpec
+if Spec::Rails::VERSION::BUILD_TIME_UTC != Spec::VERSION::BUILD_TIME_UTC
+ raise <<-EOF
+
+############################################################################
+Your RSpec on Rails plugin is incompatible with your installed RSpec.
+
+RSpec : #{Spec::VERSION::BUILD_TIME_UTC}
+RSpec on Rails : #{Spec::Rails::VERSION::BUILD_TIME_UTC}
+
+Make sure your RSpec on Rails plugin is compatible with your RSpec gem.
+See http://rspec.rubyforge.org/documentation/rails/install.html for details.
+############################################################################
+EOF
+end
|