Updated Rails to 2.1.0
[gitorious:georgyos-clone.git] / vendor / plugins / rspec_on_rails / lib / spec / rails / example / rails_example_group.rb
1 require 'spec/interop/test'
2
3 ActionView::Base.cache_template_extensions = false
4
5 module Spec
6   module Rails
7     module Example
8       class RailsExampleGroup < Test::Unit::TestCase
9         # Rails >= r8570 uses setup/teardown_fixtures explicitly
10         before(:each) do
11           setup_fixtures if self.respond_to?(:setup_fixtures)
12         end
13         after(:each) do
14           teardown_fixtures if self.respond_to?(:teardown_fixtures)
15         end
16         
17         include Spec::Rails::Matchers
18
19         @@model_id = 1000
20         # Creates a mock object instance for a +model_class+ with common
21         # methods stubbed out.
22         # Additional methods may be easily stubbed (via add_stubs) if +stubs+ is passed.
23         def mock_model(model_class, options_and_stubs = {})
24           # null = options_and_stubs.delete(:null_object)
25           # stubs = options_and_stubs
26           id = @@model_id
27           @@model_id += 1
28           options_and_stubs = {
29             :id => id,
30             :to_param => id.to_s,
31             :new_record? => false,
32             :errors => stub("errors", :count => 0)
33           }.merge(options_and_stubs)
34           m = mock("#{model_class.name}_#{id}", options_and_stubs)
35           m.send(:__mock_proxy).instance_eval <<-CODE
36             def @target.is_a?(other)
37               #{model_class}.ancestors.include?(other)
38             end
39             def @target.kind_of?(other)
40               #{model_class}.ancestors.include?(other)
41             end
42             def @target.instance_of?(other)
43               other == #{model_class}
44             end
45             def @target.class
46               #{model_class}
47             end
48           CODE
49           yield m if block_given?
50           m
51         end
52
53         #--
54         # TODO - Shouldn't this just be an extension of stub! ??
55         # - object.stub!(:method => return_value, :method2 => return_value2, :etc => etc)
56         #++
57         # Stubs methods on +object+ (if +object+ is a symbol or string a new mock
58         # with that name will be created). +stubs+ is a Hash of <tt>method=>value</tt>
59         def add_stubs(object, stubs = {}) #:nodoc:
60           m = [String, Symbol].index(object.class) ? mock(object.to_s) : object
61           stubs.each {|k,v| m.stub!(k).and_return(v)}
62           m
63         end
64         Spec::Example::ExampleGroupFactory.default(self)
65       end
66     end
67   end
68 end