| |   |
| 20 | 20 | |
| 21 | 21 | include Spec::Rails::Matchers |
| 22 | 22 | |
| 23 | | @@model_id = 1000 |
| 24 | 23 | # Creates a mock object instance for a +model_class+ with common |
| 25 | | # methods stubbed out. |
| 26 | | # Additional methods may be easily stubbed (via add_stubs) if +stubs+ is passed. |
| 24 | # methods stubbed out. Additional methods may be easily stubbed (via |
| 25 | # add_stubs) if +stubs+ is passed. |
| 27 | 26 | def mock_model(model_class, options_and_stubs = {}) |
| 28 | | id = @@model_id |
| 29 | | @@model_id += 1 |
| 27 | id = next_id |
| 30 | 28 | options_and_stubs = { |
| 31 | 29 | :id => id, |
| 32 | 30 | :to_param => id.to_s, |
| … | … | |
| 82 | 82 | # model.first_name = "David" |
| 83 | 83 | # end |
| 84 | 84 | def stub_model(model_class, stubs = {}) |
| 85 | | id = @@model_id |
| 86 | | @@model_id += 1 |
| 87 | | stubs = { |
| 88 | | :id => id |
| 89 | | }.merge(stubs) |
| 85 | stubs = {:id => next_id}.merge(stubs) |
| 90 | 86 | returning model_class.new do |model| |
| 91 | 87 | model.id = stubs.delete(:id) |
| 92 | 88 | (class << model; self; end).class_eval do |
| … | … | |
| 118 | 118 | m |
| 119 | 119 | end |
| 120 | 120 | Spec::Example::ExampleGroupFactory.default(self) |
| 121 | |
| 122 | private |
| 123 | @@model_id = 1000 |
| 124 | def next_id |
| 125 | @@model_id += 1 |
| 126 | end |
| 121 | 127 | end |
| 122 | 128 | end |
| 123 | 129 | end |
| toggle raw diff |
--- a/rspec_on_rails/lib/spec/rails/example/rails_example_group.rb
+++ b/rspec_on_rails/lib/spec/rails/example/rails_example_group.rb
@@ -20,13 +20,11 @@ module Spec
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.
+ # 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 = @@model_id
- @@model_id += 1
+ id = next_id
options_and_stubs = {
:id => id,
:to_param => id.to_s,
@@ -84,11 +82,7 @@ module Spec
# model.first_name = "David"
# end
def stub_model(model_class, stubs = {})
- id = @@model_id
- @@model_id += 1
- stubs = {
- :id => id
- }.merge(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
@@ -124,6 +118,12 @@ module Spec
m
end
Spec::Example::ExampleGroupFactory.default(self)
+
+ private
+ @@model_id = 1000
+ def next_id
+ @@model_id += 1
+ end
end
end
end |