This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stubble_spec.rb
152 lines (125 loc) · 3.92 KB
/
stubble_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require File.join(File.dirname(__FILE__), "/spec_helper")
class Model
end
describe "stubble" do
describe "class methods" do
describe "find()" do
it "returns a stubbled instance when given no args" do
model = build_stubs(Model)
Model.find("37").should equal(model)
end
it "returns a stubbled instance when given :id => the same id passed to find" do
model = build_stubs(Model, :id => "37")
Model.find("37").should equal(model)
end
it "returns a collection with :all" do
model = build_stubs(Model)
Model.find(:all).should == [model]
end
it "returns a collection with :all with additional args" do
model = build_stubs(Model)
Model.find(:all, :additional_arg => :whatever).should == [model]
end
it "raises RecordNotFound when given :id => the wrong id" do
model = build_stubs(Model, :id => "37")
lambda do
Model.find("42")
end.should raise_error(ActiveRecord::RecordNotFound)
end
end
it "stubs new on the class object, returning an stubbled instance" do
model = build_stubs(Model)
Model.new.should equal(model)
end
it "stubs all on the class object, returning an stubbled instance in an array" do
model = build_stubs(Model)
Model.all.should == [model]
end
end
context "stubbing" do
it "yields the instance" do
stubbing(Model) do |model|
model.should_not be_nil
end
end
it "tears down the stubs" do
model = nil
stubbing(Model) do |model|
model.update_attribute.should be(true)
end
expect do
model.update_attribute
end.to raise_error(/undefined local variable or method `update_attribute'/)
end
end
context "creating" do
it "stubs new" do
instance = build_stubs(Model)
Model.new.should equal(instance)
end
it "stubs create" do
instance = build_stubs(Model)
Model.create.should equal(instance)
end
it "stubs create!" do
instance = build_stubs(Model)
Model.create!.should equal(instance)
end
it "raises if valid => false" do
build_stubs(Model, :as => :invalid)
expect { Model.create! }.to raise_error(ActiveRecord::RecordInvalid)
end
end
describe "instances" do
context "default (valid)" do
def valid_model
@valid_model = build_stubs(Model)
end
it "returns true for save" do
valid_model.save.should == true
end
it "returns true for save!" do
valid_model.save!.should == true
end
it "returns true for update_attribute" do
valid_model.update_attribute.should == true
end
it "returns true for update_attributes" do
valid_model.update_attributes.should == true
end
it "returns true for update_attributes!" do
valid_model.update_attributes!.should == true
end
it "returns true for valid?" do
valid_model.valid?.should == true
end
end
context "invalid_model" do
def invalid_model
@invalid_model = build_stubs(Model, :as => :invalid)
end
it "returns false for save" do
invalid_model.save.should be_false
end
it "raises on save!" do
lambda {
invalid_model.save!
}.should raise_error(ActiveRecord::RecordInvalid)
end
it "returns false for update_attribute" do
invalid_model.update_attribute.should be_false
end
it "returns false for update_attributes" do
invalid_model.update_attributes.should be_false
end
it "raises on update_attributes!" do
lambda {
invalid_model.update_attributes!
}.should raise_error(ActiveRecord::RecordInvalid)
end
it "returns false for valid?" do
invalid_model.valid?.should be_false
end
end
end
end