public
Fork of dchelimsky/rspec
Description: Behaviour Driven Development framework for Ruby
Homepage: http://rspec.info
Clone URL: git://github.com/martinbtt/rspec.git
added stub_model method
dchelimsky (author)
Fri Mar 07 22:53:03 -0800 2008
commit  0a7fae432fea101ab8292ab5a5affb4f1e5f3008
tree    228e76417457db13368fc1c95fb83656ae6a4731
parent  64713d3aeeda351d5b07053b16892d7b39b2cbc7
...
1
2
3
 
4
5
6
...
1
2
3
4
5
6
7
0
@@ -1,6 +1,7 @@
0
 class CreateMockables < ActiveRecord::Migration
0
   def self.up
0
     create_table :mockable_models do |t|
0
+ t.column :name, :string
0
     end
0
     create_table :associated_models do |t|
0
       t.column :mockable_model_id, :integer
...
1
2
 
3
4
5
6
 
 
 
7
8
9
...
21
22
23
24
25
26
27
28
...
49
50
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
53
54
...
1
2
3
4
5
6
7
8
9
10
11
12
13
...
25
26
27
 
 
28
29
30
...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
0
@@ -1,9 +1,13 @@
0
 require 'spec/interop/test'
0
 
0
+
0
 ActionView::Base.cache_template_extensions = false
0
 
0
 module Spec
0
   module Rails
0
+
0
+ class IllegalDataAccessException < StandardError; end
0
+
0
     module Example
0
       class RailsExampleGroup < Test::Unit::TestCase
0
         # Rails >= r8570 uses setup/teardown_fixtures explicitly
0
@@ -21,8 +25,6 @@ module Spec
0
         # methods stubbed out.
0
         # Additional methods may be easily stubbed (via add_stubs) if +stubs+ is passed.
0
         def mock_model(model_class, options_and_stubs = {})
0
- # null = options_and_stubs.delete(:null_object)
0
- # stubs = options_and_stubs
0
           id = @@model_id
0
           @@model_id += 1
0
           options_and_stubs = {
0
@@ -49,6 +51,34 @@ module Spec
0
           yield m if block_given?
0
           m
0
         end
0
+
0
+ # Creates an instance of a +model_class+ that is prohibited
0
+ # from accessing the database. It comes with an id, although
0
+ # you can stub the id explicitly.
0
+ #
0
+ # == Examples
0
+ #
0
+ # stub_model(Person)
0
+ # stub_model(Person, :id => 37)
0
+ # stub_model(Person) do |person|
0
+ # model.first_name = "David"
0
+ # end
0
+ def stub_model(model_class, stubs = {})
0
+ id = @@model_id
0
+ @@model_id += 1
0
+ stubs = {
0
+ :id => id
0
+ }.merge(stubs)
0
+ returning model_class.new do |model|
0
+ add_stubs(model, stubs)
0
+ (class << model; self; end).class_eval do
0
+ def connection
0
+ raise IllegalDataAccessException.new("stubbed models are not allowed to access the database")
0
+ end
0
+ end
0
+ yield model if block_given?
0
+ end
0
+ end
0
 
0
         #--
0
         # TODO - Shouldn't this just be an extension of stub! ??
...
1
2
3
4
5
6
7
8
9
10
11
12
 
13
14
15
...
1
 
 
 
 
 
 
 
 
 
 
 
2
3
4
5
0
@@ -1,15 +1,5 @@
0
 require File.dirname(__FILE__) + '/../../spec_helper'
0
-
0
-class MockableModel < ActiveRecord::Base
0
- has_one :associated_model
0
-end
0
-
0
-class SubMockableModel < MockableModel
0
-end
0
-
0
-class AssociatedModel < ActiveRecord::Base
0
- belongs_to :mockable_model
0
-end
0
+require File.dirname(__FILE__) + '/ar_classes'
0
 
0
 describe "mock_model", :type => :view do
0
   before(:each) do

Comments

    No one has commented yet.