dropio-mock_record 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +4 -0
- data/lib/mock_records.rb +7 -0
- data/lib/mock_records/mock_record.rb +50 -0
- data/lib/mock_records/mock_record_argument_matcher.rb +21 -0
- data/lib/mock_records/mock_record_stubbing.rb +25 -0
- data/spec/mock_records/mock_record_argument_matcher_spec.rb +22 -0
- data/spec/mock_records/mock_record_spec.rb +90 -0
- data/spec/mock_records/mock_record_stubbing_spec.rb +70 -0
- data/spec/spec_helper.rb +5 -0
- metadata +65 -0
data/VERSION.yml
ADDED
data/lib/mock_records.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module MockRecords
|
4
|
+
class MockRecord
|
5
|
+
class << self
|
6
|
+
def create!(attrs={})
|
7
|
+
mr = new(attrs)
|
8
|
+
mr.save!
|
9
|
+
mr
|
10
|
+
end
|
11
|
+
alias_method :create, :create!
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(attrs={})
|
15
|
+
@data = OpenStruct.new(attrs)
|
16
|
+
@saved_data = OpenStruct.new
|
17
|
+
@saved = false
|
18
|
+
@dirty = false
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(id, *a)
|
22
|
+
@dirty = true if id.to_s =~ /=$/
|
23
|
+
@data.send(id, *a)
|
24
|
+
end
|
25
|
+
|
26
|
+
def saved?
|
27
|
+
@saved
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_record?
|
31
|
+
not saved?
|
32
|
+
end
|
33
|
+
|
34
|
+
def dirty?
|
35
|
+
@dirty
|
36
|
+
end
|
37
|
+
|
38
|
+
def save!
|
39
|
+
@saved = true
|
40
|
+
@dirty = false
|
41
|
+
@saved_data = @data.dup
|
42
|
+
end
|
43
|
+
alias_method :save, :save!
|
44
|
+
|
45
|
+
def reload
|
46
|
+
@data = @saved_data.dup
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module MockRecords
|
2
|
+
class MockRecordArgumentMatcher
|
3
|
+
def initialize(attrs)
|
4
|
+
@attrs = attrs
|
5
|
+
end
|
6
|
+
|
7
|
+
def ==(mock)
|
8
|
+
@attrs.all? do |k, v|
|
9
|
+
mock.send(k) == v
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def description
|
14
|
+
"mock_record_with(#{@attrs.inspect})"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def mock_record_with(attrs)
|
19
|
+
MockRecordArgumentMatcher.new(attrs)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec'
|
2
|
+
|
3
|
+
module MockRecords
|
4
|
+
module MockRecordStubbing
|
5
|
+
def stub_new_records(klass)
|
6
|
+
mock_records = Array.new
|
7
|
+
|
8
|
+
make_new_record = lambda do |method, attrs|
|
9
|
+
mr = MockRecord.send(method, attrs)
|
10
|
+
mock_records << mr
|
11
|
+
mr
|
12
|
+
end
|
13
|
+
|
14
|
+
# Stub creation methods
|
15
|
+
[:new, :create, :create!].each do |method|
|
16
|
+
klass.stub!(method).with().and_return { make_new_record[method, {}] }
|
17
|
+
klass.stub!(method).with(an_instance_of(Hash)).and_return { |attrs| make_new_record[method, attrs] }
|
18
|
+
end
|
19
|
+
|
20
|
+
klass.stub!(:new_records).and_return do
|
21
|
+
mock_records.select { |mr| mr.saved? }.each { |mr| mr.reload }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
class Model; end
|
4
|
+
|
5
|
+
describe MockRecords::MockRecordArgumentMatcher do
|
6
|
+
include MockRecords
|
7
|
+
|
8
|
+
it "matches a MockRecord with the given attributes" do
|
9
|
+
should_receive(:a_message).with(mock_record_with(:name => "Joe"))
|
10
|
+
a_message MockRecords::MockRecord.new(:name => "Joe")
|
11
|
+
end
|
12
|
+
|
13
|
+
it "matches a MockRecord with the given attributes and more" do
|
14
|
+
should_receive(:a_message).with(mock_record_with(:name => "Joe"))
|
15
|
+
a_message MockRecords::MockRecord.new(:name => "Joe", :age => 32)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "doesn't match a MockRecord missing any attribute" do
|
19
|
+
should_not_receive(:a_message).with(mock_record_with(:name => "Joe", :age => 32))
|
20
|
+
a_message MockRecords::MockRecord.new(:name => "Joe")
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
module MockRecords
|
4
|
+
describe MockRecord do
|
5
|
+
it "lets any attribute be set" do
|
6
|
+
lambda {
|
7
|
+
MockRecord.new.any_attribute_at_all = :a_value
|
8
|
+
}.should_not raise_error(NoMethodError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "recalls any set attribute" do
|
12
|
+
mr = MockRecord.new
|
13
|
+
mr.any_attribute_at_all = :a_value
|
14
|
+
mr.any_attribute_at_all.should == :a_value
|
15
|
+
end
|
16
|
+
|
17
|
+
it "begins as a new record (unsaved)" do
|
18
|
+
MockRecord.new.should be_a_new_record
|
19
|
+
MockRecord.new.should_not be_saved
|
20
|
+
end
|
21
|
+
|
22
|
+
it "is saved (no longer a new record) once saved" do
|
23
|
+
mr = MockRecord.new
|
24
|
+
mr.save!
|
25
|
+
mr.should_not be_a_new_record
|
26
|
+
mr.should be_saved
|
27
|
+
end
|
28
|
+
|
29
|
+
it "remains saved when a value is changed" do
|
30
|
+
mr = MockRecord.new
|
31
|
+
mr.save!
|
32
|
+
mr.any_attribute_at_all = :a_value
|
33
|
+
mr.should_not be_a_new_record
|
34
|
+
mr.should be_saved
|
35
|
+
end
|
36
|
+
|
37
|
+
it "begins not dirty" do
|
38
|
+
MockRecord.new.should_not be_dirty
|
39
|
+
end
|
40
|
+
|
41
|
+
it "is dirty after a value is set" do
|
42
|
+
mr = MockRecord.new
|
43
|
+
mr.any_attribute_at_all = :a_value
|
44
|
+
mr.should be_dirty
|
45
|
+
end
|
46
|
+
|
47
|
+
it "is no longer dirty once saved" do
|
48
|
+
mr = MockRecord.new
|
49
|
+
mr.any_attribute_at_all = :a_value
|
50
|
+
mr.save!
|
51
|
+
mr.should_not be_dirty
|
52
|
+
end
|
53
|
+
|
54
|
+
it "remains not dirty when values are read" do
|
55
|
+
mr = MockRecord.new
|
56
|
+
mr.any_attribute_at_all = :a_value
|
57
|
+
mr.save!
|
58
|
+
mr.any_attribute_at_all
|
59
|
+
mr.should_not be_dirty
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#reload" do
|
63
|
+
it "rolls back unsaved changes" do
|
64
|
+
mr = MockRecord.new
|
65
|
+
mr.any_attribute_at_all = :a_value
|
66
|
+
mr.save!
|
67
|
+
mr.any_attribute_at_all = :another_value
|
68
|
+
mr.reload
|
69
|
+
|
70
|
+
mr.any_attribute_at_all.should == :a_value
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe ".new" do
|
75
|
+
it "takes a hash of attributes" do
|
76
|
+
MockRecord.new(:name => "Joe").name.should == "Joe"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe ".create!" do
|
81
|
+
it "creates a saved MockRecord" do
|
82
|
+
MockRecord.create!.should be_saved
|
83
|
+
end
|
84
|
+
|
85
|
+
it "takes a hash of attributes" do
|
86
|
+
MockRecord.create!(:name => "Joe").name.should == "Joe"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
class Model; end
|
4
|
+
|
5
|
+
describe "When Model is stubbed with MockRecordStubbing#stub_new_records, Model" do
|
6
|
+
include MockRecords
|
7
|
+
|
8
|
+
before do
|
9
|
+
stub_new_records Model
|
10
|
+
end
|
11
|
+
|
12
|
+
describe ".new" do
|
13
|
+
it "returns a new mock record" do
|
14
|
+
Model.new.should be_a_new_record
|
15
|
+
end
|
16
|
+
|
17
|
+
it "takes a hash of attributes to set" do
|
18
|
+
m = Model.new(:name => "gary", :age => 32)
|
19
|
+
m.name.should == "gary"
|
20
|
+
m.age.should == 32
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe ".create" do
|
25
|
+
it "returns a saved mock record" do
|
26
|
+
Model.create.should be_saved
|
27
|
+
end
|
28
|
+
|
29
|
+
it "takes a hash of attributes to set" do
|
30
|
+
m = Model.create(:name => "gary", :age => 32)
|
31
|
+
m.name.should == "gary"
|
32
|
+
m.age.should == 32
|
33
|
+
m.should be_saved
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe ".create!" do
|
38
|
+
it "returns a saved mock record" do
|
39
|
+
Model.create!.should be_saved
|
40
|
+
end
|
41
|
+
|
42
|
+
it "takes a hash of attributes to set" do
|
43
|
+
m = Model.create!(:name => "gary", :age => 32)
|
44
|
+
m.name.should == "gary"
|
45
|
+
m.age.should == 32
|
46
|
+
m.should be_saved
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe ".new_records" do
|
51
|
+
it "returns all of the saved mock records" do
|
52
|
+
model1 = Model.new
|
53
|
+
model2 = Model.create
|
54
|
+
model3 = Model.new
|
55
|
+
|
56
|
+
model3.save
|
57
|
+
|
58
|
+
Model.new_records.should == [model2, model3]
|
59
|
+
end
|
60
|
+
|
61
|
+
it "does not reflect unsaved changes" do
|
62
|
+
model = Model.new
|
63
|
+
model.age = 32
|
64
|
+
model.save
|
65
|
+
model.age = 33
|
66
|
+
|
67
|
+
Model.new_records.first.age.should == 32
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dropio-mock_record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Jaros
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-26 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A mock object which stands in for ORM objects.
|
17
|
+
email: peeja@dropio.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- lib/mock_records
|
27
|
+
- lib/mock_records/mock_record.rb
|
28
|
+
- lib/mock_records/mock_record_argument_matcher.rb
|
29
|
+
- lib/mock_records/mock_record_stubbing.rb
|
30
|
+
- lib/mock_records.rb
|
31
|
+
- spec/mock_records
|
32
|
+
- spec/mock_records/mock_record_argument_matcher_spec.rb
|
33
|
+
- spec/mock_records/mock_record_spec.rb
|
34
|
+
- spec/mock_records/mock_record_stubbing_spec.rb
|
35
|
+
- spec/spec.opts
|
36
|
+
- spec/spec_helper.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage:
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --inline-source
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: A mock object which stands in for ORM objects.
|
64
|
+
test_files: []
|
65
|
+
|