| 1 |
require 'spec/interop/test' |
| 2 |
|
| 3 |
|
| 4 |
ActionView::Base.cache_template_extensions = false |
| 5 |
|
| 6 |
module Spec |
| 7 |
module Rails |
| 8 |
|
| 9 |
class IllegalDataAccessException < StandardError; end |
| 10 |
|
| 11 |
module Example |
| 12 |
class RailsExampleGroup < Test::Unit::TestCase |
| 13 |
|
| 14 |
before(:each) do |
| 15 |
setup_fixtures if self.respond_to?(:setup_fixtures) |
| 16 |
end |
| 17 |
after(:each) do |
| 18 |
teardown_fixtures if self.respond_to?(:teardown_fixtures) |
| 19 |
end |
| 20 |
|
| 21 |
include Spec::Rails::Matchers |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
def mock_model(model_class, options_and_stubs = {}) |
| 27 |
id = next_id |
| 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 |
|
| 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 |
def stub_model(model_class, stubs = {}) |
| 85 |
stubs = {:id => next_id}.merge(stubs) |
| 86 |
returning model_class.new do |model| |
| 87 |
model.id = stubs.delete(:id) |
| 88 |
(class << model; self; end).class_eval do |
| 89 |
def connection |
| 90 |
raise IllegalDataAccessException.new("stubbed models are not allowed to access the database") |
| 91 |
end |
| 92 |
def new_record? |
| 93 |
id.nil? |
| 94 |
end |
| 95 |
def as_new_record |
| 96 |
self.id = nil |
| 97 |
self |
| 98 |
end |
| 99 |
end |
| 100 |
stubs.each do |k,v| |
| 101 |
if model.has_attribute?(k) |
| 102 |
model[k] = stubs.delete(k) |
| 103 |
end |
| 104 |
end |
| 105 |
add_stubs(model, stubs) |
| 106 |
yield model if block_given? |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
def add_stubs(object, stubs = {}) |
| 116 |
m = [String, Symbol].index(object.class) ? mock(object.to_s) : object |
| 117 |
stubs.each {|k,v| m.stub!(k).and_return(v)} |
| 118 |
m |
| 119 |
end |
| 120 |
Spec::Example::ExampleGroupFactory.default(self) |
| 121 |
|
| 122 |
private |
| 123 |
@@model_id = 1000 |
| 124 |
def next_id |
| 125 |
@@model_id += 1 |
| 126 |
end |
| 127 |
end |
| 128 |
end |
| 129 |
end |
| 130 |
end |