mocked_fixtures 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/History.txt +4 -0
- data/License.txt +20 -0
- data/Manifest.txt +49 -0
- data/README.txt +206 -0
- data/Rakefile +4 -0
- data/TODO.txt +5 -0
- data/config/hoe.rb +73 -0
- data/config/requirements.rb +15 -0
- data/init.rb +2 -0
- data/lib/mocked_fixtures/connection_adapters/sqlserver_adapter.rb +25 -0
- data/lib/mocked_fixtures/mock_connection.rb +61 -0
- data/lib/mocked_fixtures/mock_factory.rb +12 -0
- data/lib/mocked_fixtures/mock_fixtures.rb +58 -0
- data/lib/mocked_fixtures/mocks/flexmock.rb +35 -0
- data/lib/mocked_fixtures/mocks/mocha.rb +47 -0
- data/lib/mocked_fixtures/mocks/rspec.rb +40 -0
- data/lib/mocked_fixtures/schema_parser.rb +30 -0
- data/lib/mocked_fixtures/spec/configuration.rb +19 -0
- data/lib/mocked_fixtures/testcase.rb +122 -0
- data/lib/mocked_fixtures/version.rb +9 -0
- data/lib/mocked_fixtures.rb +15 -0
- data/script/console +10 -0
- data/script/txt2html +82 -0
- data/spec/connection_adapters/sqlserver_adapter_spec.rb +44 -0
- data/spec/mock_connection_spec.rb +6 -0
- data/spec/mock_factory_spec.rb +59 -0
- data/spec/mock_fixtures_spec.rb +30 -0
- data/spec/resources/companies.yml +3 -0
- data/spec/resources/company.rb +5 -0
- data/spec/resources/employee.rb +3 -0
- data/spec/resources/employees.yml +8 -0
- data/spec/resources/schema.rb +18 -0
- data/spec/rspec-rails/MIT-LICENSE +31 -0
- data/spec/rspec-rails/mocks.rb +115 -0
- data/spec/rspec-rails/rails_example_group.rb +27 -0
- data/spec/rspec-rails/rspec-rails.rb +15 -0
- data/spec/schema_parser_spec.rb +41 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/testcase_spec.rb +73 -0
- data/tasks/deployment.rake +34 -0
- data/tasks/environment.rake +7 -0
- data/tasks/rspec.rake +21 -0
- data/tasks/website.rake +17 -0
- data/website/index.html +86 -0
- data/website/index.txt +81 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.html.erb +48 -0
- metadata +117 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Adam Meehan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
TODO.txt
|
7
|
+
config/hoe.rb
|
8
|
+
config/requirements.rb
|
9
|
+
init.rb
|
10
|
+
lib/mocked_fixtures.rb
|
11
|
+
lib/mocked_fixtures/connection_adapters/sqlserver_adapter.rb
|
12
|
+
lib/mocked_fixtures/mock_connection.rb
|
13
|
+
lib/mocked_fixtures/mock_factory.rb
|
14
|
+
lib/mocked_fixtures/mock_fixtures.rb
|
15
|
+
lib/mocked_fixtures/mocks/flexmock.rb
|
16
|
+
lib/mocked_fixtures/mocks/mocha.rb
|
17
|
+
lib/mocked_fixtures/mocks/rspec.rb
|
18
|
+
lib/mocked_fixtures/schema_parser.rb
|
19
|
+
lib/mocked_fixtures/spec/configuration.rb
|
20
|
+
lib/mocked_fixtures/testcase.rb
|
21
|
+
lib/mocked_fixtures/version.rb
|
22
|
+
script/console
|
23
|
+
script/txt2html
|
24
|
+
spec/connection_adapters/sqlserver_adapter_spec.rb
|
25
|
+
spec/mock_connection_spec.rb
|
26
|
+
spec/mock_factory_spec.rb
|
27
|
+
spec/mock_fixtures_spec.rb
|
28
|
+
spec/resources/companies.yml
|
29
|
+
spec/resources/company.rb
|
30
|
+
spec/resources/employee.rb
|
31
|
+
spec/resources/employees.yml
|
32
|
+
spec/resources/schema.rb
|
33
|
+
spec/rspec-rails/MIT-LICENSE
|
34
|
+
spec/rspec-rails/mocks.rb
|
35
|
+
spec/rspec-rails/rails_example_group.rb
|
36
|
+
spec/rspec-rails/rspec-rails.rb
|
37
|
+
spec/schema_parser_spec.rb
|
38
|
+
spec/spec.opts
|
39
|
+
spec/spec_helper.rb
|
40
|
+
spec/testcase_spec.rb
|
41
|
+
tasks/deployment.rake
|
42
|
+
tasks/environment.rake
|
43
|
+
tasks/rspec.rake
|
44
|
+
tasks/website.rake
|
45
|
+
website/index.html
|
46
|
+
website/index.txt
|
47
|
+
website/javascripts/rounded_corners_lite.inc.js
|
48
|
+
website/stylesheets/screen.css
|
49
|
+
website/template.html.erb
|
data/README.txt
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
= mocked_fixtures
|
2
|
+
|
3
|
+
* Project page: http://mockedfixtures.rubyforge.org/
|
4
|
+
* Tickets: http://adzap.lighthouseapp.com/projects/16416-mocked_fixtures
|
5
|
+
* Contribute: http://github.com/adzap/mocked_fixtures
|
6
|
+
* Discuss: http://groups.google.com/group/mocked_fixtures
|
7
|
+
* Blog: http://duckpunching.com/mocked_fixtures
|
8
|
+
|
9
|
+
|
10
|
+
== DESCRIPTION:
|
11
|
+
|
12
|
+
Yes, lets laugh at those silly fixtures with their silly yaml or comma
|
13
|
+
separated values! Ha ha ha! Don't they know how passe they are. All the cool kids
|
14
|
+
are mocking and stubbing. Get with the times fixtures!
|
15
|
+
|
16
|
+
Well actually fixtures still have their place. Especially with the foxy fixtures
|
17
|
+
extensions added to Rails 2.0, they are now much easier to use. Problem is the
|
18
|
+
undesirable database overhead that comes with reloading your fixtures for every
|
19
|
+
test. Also, in controller and view tests, the database layer is unnecessary and
|
20
|
+
using mock objects for test isolation is often preferred.
|
21
|
+
|
22
|
+
This poses another challenge of the often tedious creation those mocks to return
|
23
|
+
the values you need for tests much like you did for your fixtures. But what if
|
24
|
+
you could reuse those fixtures as mocks where you don't need real ActiveRecord
|
25
|
+
objects. Well thats where mocked_fixtures comes in.
|
26
|
+
|
27
|
+
This plugin helps by allowing you use your fixtures as disconnected mock objects
|
28
|
+
which never touch the database when loaded or used. So you can now keep using
|
29
|
+
those thoughtfully crafted fixtures in your controller and view tests but minus
|
30
|
+
the database overhead.
|
31
|
+
|
32
|
+
A mocked fixture is a mock model object with all the model attributes stubbed
|
33
|
+
out to return the values from the fixture if defined. If no fixture value was
|
34
|
+
defined then a nil is returned for the attribute method as you would expect.
|
35
|
+
There are no attribute setters defined on the object as they are left for you
|
36
|
+
to do as necessary.
|
37
|
+
|
38
|
+
The attributes for each model are read from the schema.rb file to avoid any
|
39
|
+
database access at all!
|
40
|
+
|
41
|
+
== FEATURES/PROBLEMS:
|
42
|
+
|
43
|
+
* Reuse your fixtures as mock model objects without database overhead
|
44
|
+
* Adds feature to supported mocking libraries to quickly create empty mocked
|
45
|
+
model with all attributes methods stubbed out. Supported are Rspec, flexmock
|
46
|
+
and mocha.
|
47
|
+
* Same familiar style of using regular fixtures
|
48
|
+
* Works with popular testing frameworks such as Rspec (with rspec-rails),
|
49
|
+
shoulda and any testing library which uses good old test/unit as its base.
|
50
|
+
|
51
|
+
What it doesn't do:
|
52
|
+
|
53
|
+
* Touch the database. This means that the fixtures are not inserted in the
|
54
|
+
tables, thats not what the plugin is for.
|
55
|
+
* Create mock objects with the attribute setters. Only reader methods.
|
56
|
+
|
57
|
+
|
58
|
+
Disclaimer:
|
59
|
+
It is worth noting that the plugin is highly coupled to the Rails fixtures
|
60
|
+
implementation and so would likely break with future changes to the fixtures API.
|
61
|
+
|
62
|
+
== SYNOPSIS:
|
63
|
+
|
64
|
+
To get going you installed as a gem you need to require the plugin at the top
|
65
|
+
of your test_helper or spec_helper file. If you installed as a Rails plugin then
|
66
|
+
you can skip that step.
|
67
|
+
|
68
|
+
If you are using test/unit then you need specify which mocking library you
|
69
|
+
are using in your test_helper like so:
|
70
|
+
|
71
|
+
class Test::Unit::TestCase
|
72
|
+
|
73
|
+
# can be one of :flexmock or :mocha
|
74
|
+
self.mock_fixtures_with :flexmock
|
75
|
+
end
|
76
|
+
|
77
|
+
If you are using Rspec you need to set it in the configure block:
|
78
|
+
|
79
|
+
Spec::Runner.configure do |config|
|
80
|
+
|
81
|
+
# can be one of :rspec, :flexmock or :mocha
|
82
|
+
config.mock_fixtures_with :rspec
|
83
|
+
end
|
84
|
+
|
85
|
+
If you are using something other than the supported libraries then you get an
|
86
|
+
error alerting the plugin can't be used with that library. You can then write you own
|
87
|
+
interface to the plugin for that library if you want. See the files in
|
88
|
+
lib/mocked_fixtures/mocks and the spec for mock fixture objects in spec/mock_factory_spec.rb.
|
89
|
+
|
90
|
+
On to the good stuff. Now if you have a Company model and fixture for the model
|
91
|
+
like this
|
92
|
+
|
93
|
+
megacorp:
|
94
|
+
name: Mega Corporation
|
95
|
+
moto: Do Evil
|
96
|
+
|
97
|
+
Just like normal fixtures you declare which ones you want to use in your test
|
98
|
+
or spec at the top
|
99
|
+
|
100
|
+
class MyTest < Test::Unit:TestCase
|
101
|
+
mock_fixtures :companies
|
102
|
+
|
103
|
+
def setup
|
104
|
+
@company = mock_companies(:megacorp)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_does_something
|
108
|
+
assert_equal @company.moto, 'Do Evil'
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
All the attributes will be stubbed to return the values from the fixture. The
|
113
|
+
fixture is generated using the internal Rails fixtures class, so any of fixture
|
114
|
+
tricks, such as association labels and automatically generated ids, will work.
|
115
|
+
|
116
|
+
If you want more than one fixture, then like normal fixtures you list the
|
117
|
+
fixture keys to get back an array of the objects.
|
118
|
+
|
119
|
+
@companies = mock_companies(:megacorp, :bigstuff)
|
120
|
+
|
121
|
+
|
122
|
+
To quickly grab all of the fixtures you call use the :all option
|
123
|
+
|
124
|
+
@companies = mock_companies(:all)
|
125
|
+
|
126
|
+
|
127
|
+
Like regular fixtures you can also declare global mock fixtures to pre-load for all
|
128
|
+
your tests. Because the mock fixtures don't access the database it doesn't slow down
|
129
|
+
the running of tests like global fixtures can.
|
130
|
+
|
131
|
+
To setup global mock fixtures, in your test_helper put:
|
132
|
+
|
133
|
+
class Test::Unit::TestCase
|
134
|
+
|
135
|
+
self.global_mock_fixtures = :companies
|
136
|
+
end
|
137
|
+
|
138
|
+
If you are using Rspec you need to set it in the configure block:
|
139
|
+
|
140
|
+
Spec::Runner.configure do |config|
|
141
|
+
|
142
|
+
config.global_mock_fixtures = :companies
|
143
|
+
end
|
144
|
+
|
145
|
+
You can also set the global to :all and all fixtures will be loaded as mocks. This
|
146
|
+
can add some test startup time if you have a lot of fixtures but shouldn't slow
|
147
|
+
down your tests when running at all. Might use a bit of memory though.
|
148
|
+
|
149
|
+
Thats all for now, so mock on!
|
150
|
+
|
151
|
+
|
152
|
+
NOTE:
|
153
|
+
If you use MS SQL Server then you need to apply a fix for this adapter to
|
154
|
+
correctly dump primary keys when not named 'id'. Just drop this line in an
|
155
|
+
initializer file
|
156
|
+
|
157
|
+
require 'mocked_fixtures/connection_adapters/sqlserver_adapter'
|
158
|
+
|
159
|
+
You then need to do a dump. Um, keep your pants on, I mean a database dump with
|
160
|
+
|
161
|
+
rake db:schema:dump
|
162
|
+
|
163
|
+
Now the schema.rb file should be complete.
|
164
|
+
|
165
|
+
== REQUIREMENTS:
|
166
|
+
|
167
|
+
Rails 2.x
|
168
|
+
|
169
|
+
At least one of:
|
170
|
+
|
171
|
+
* Spec::Mock (included with Rspec)
|
172
|
+
* flexmock
|
173
|
+
* mocha
|
174
|
+
|
175
|
+
== INSTALL:
|
176
|
+
|
177
|
+
* sudo gem install mocked_fixtures
|
178
|
+
|
179
|
+
Or as a plugin
|
180
|
+
|
181
|
+
* ./script/plugin install git://github.com/adzap/mocked_fixtures.git
|
182
|
+
|
183
|
+
== LICENSE:
|
184
|
+
|
185
|
+
(The MIT License)
|
186
|
+
|
187
|
+
Copyright (c) 2008 Adam Meehan
|
188
|
+
|
189
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
190
|
+
a copy of this software and associated documentation files (the
|
191
|
+
'Software'), to deal in the Software without restriction, including
|
192
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
193
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
194
|
+
permit persons to whom the Software is furnished to do so, subject to
|
195
|
+
the following conditions:
|
196
|
+
|
197
|
+
The above copyright notice and this permission notice shall be
|
198
|
+
included in all copies or substantial portions of the Software.
|
199
|
+
|
200
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
201
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
202
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
203
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
204
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
205
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
206
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/TODO.txt
ADDED
data/config/hoe.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'mocked_fixtures/version'
|
2
|
+
|
3
|
+
AUTHOR = 'Adam Meehan' # can also be an array of Authors
|
4
|
+
EMAIL = "adam.meehan@gmail.com"
|
5
|
+
DESCRIPTION = "Use Rails fixtures as mocked model objects without database access"
|
6
|
+
GEM_NAME = 'mocked_fixtures' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'mockedfixtures' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
EXTRA_DEPENDENCIES = [
|
11
|
+
# ['activesupport', '>= 1.3.1']
|
12
|
+
] # An array of rubygem dependencies [name, version]
|
13
|
+
|
14
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
15
|
+
@config = nil
|
16
|
+
RUBYFORGE_USERNAME = "adzap"
|
17
|
+
def rubyforge_username
|
18
|
+
unless @config
|
19
|
+
begin
|
20
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
21
|
+
rescue
|
22
|
+
puts <<-EOS
|
23
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
24
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
25
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
26
|
+
EOS
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
REV = nil
|
35
|
+
# UNCOMMENT IF REQUIRED:
|
36
|
+
# REV = YAML.load(`svn info`)['Revision']
|
37
|
+
VERS = MockedFixtures::VERSION::STRING + (REV ? ".#{REV}" : "")
|
38
|
+
RDOC_OPTS = ['--quiet', '--title', 'mocked_fixtures documentation',
|
39
|
+
"--opname", "index.html",
|
40
|
+
"--line-numbers",
|
41
|
+
"--main", "README",
|
42
|
+
"--inline-source"]
|
43
|
+
|
44
|
+
class Hoe
|
45
|
+
def extra_deps
|
46
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
47
|
+
@extra_deps
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generate all the Rake tasks
|
52
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
53
|
+
$hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
54
|
+
p.developer(AUTHOR, EMAIL)
|
55
|
+
p.description = DESCRIPTION
|
56
|
+
p.summary = DESCRIPTION
|
57
|
+
p.url = HOMEPATH
|
58
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
59
|
+
p.test_globs = ["test/**/test_*.rb"]
|
60
|
+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
61
|
+
|
62
|
+
# == Optional
|
63
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
64
|
+
#p.extra_deps = EXTRA_DEPENDENCIES
|
65
|
+
|
66
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
67
|
+
end
|
68
|
+
|
69
|
+
CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
70
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
71
|
+
$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
72
|
+
$hoe.rsync_args = '-av --delete --ignore-errors'
|
73
|
+
$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
data/init.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# This adds the method to find the primary key for the schema dumper so that
|
2
|
+
# the schema will correctly define the primary key. Need to get this into the
|
3
|
+
# real adapter obviously, though the project seems almost dead.
|
4
|
+
module MockedFixtures
|
5
|
+
module ConnectionAdapters
|
6
|
+
module SQLServerAdapter
|
7
|
+
|
8
|
+
# Returns a table's primary key and belonging sequence (not applicable to SQL server).
|
9
|
+
def pk_and_sequence_for(table_name)
|
10
|
+
@connection["AutoCommit"] = false
|
11
|
+
keys = []
|
12
|
+
execute("EXEC sp_helpindex '#{table_name}'") do |handle|
|
13
|
+
if handle.column_info.any?
|
14
|
+
pk_index = handle.detect {|index| index[1] =~ /primary key/ }
|
15
|
+
keys << pk_index[2] if pk_index
|
16
|
+
end
|
17
|
+
end
|
18
|
+
keys.length == 1 ? [keys.first, nil] : nil
|
19
|
+
ensure
|
20
|
+
@connection["AutoCommit"] = true
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module MockedFixtures
|
2
|
+
class MockConnection
|
3
|
+
attr_accessor :schema, :loaded_fixtures, :current_fixture_label
|
4
|
+
|
5
|
+
# mock column class which is used in Fixtures class insert_fixtures method
|
6
|
+
class Column
|
7
|
+
attr_accessor :name, :type
|
8
|
+
def initialize(name, type)
|
9
|
+
@name, @type = name, type
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@schema = @@schema ||= MockedFixtures::SchemaParser.load_schema
|
15
|
+
@columns = {}
|
16
|
+
@loaded_fixtures = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# stores full fixture after association values loaded and type casting
|
20
|
+
def insert_fixture(fixture, table_name)
|
21
|
+
loaded_fixtures[table_name] ||= {}
|
22
|
+
loaded_fixtures[table_name][@current_fixture_label] = type_cast_fixture(fixture, table_name)
|
23
|
+
end
|
24
|
+
|
25
|
+
def type_cast_fixture(fixture, table_name)
|
26
|
+
fixture.to_hash.inject({}) do |new_hash, row|
|
27
|
+
begin
|
28
|
+
type = @schema[table_name.to_s][:columns].assoc(row[0])[1]
|
29
|
+
new_hash[row[0].to_sym] = type_cast_value(type, row[1])
|
30
|
+
new_hash
|
31
|
+
rescue
|
32
|
+
raise "Mock fixture key '#{row[0]}' not found in schema '#{table_name}'"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Modified from ActiveRecord::Column class.
|
38
|
+
def type_cast_value(type, value)
|
39
|
+
return nil if value.nil?
|
40
|
+
column = ActiveRecord::ConnectionAdapters::Column
|
41
|
+
case type.to_sym
|
42
|
+
when :string then value
|
43
|
+
when :text then value
|
44
|
+
when :integer then value.to_i rescue value ? 1 : 0
|
45
|
+
when :float then value.to_f
|
46
|
+
when :decimal then column.value_to_decimal(value)
|
47
|
+
when :datetime then column.string_to_time(value)
|
48
|
+
when :timestamp then column.string_to_time(value)
|
49
|
+
when :time then column.string_to_dummy_time(value)
|
50
|
+
when :date then column.string_to_date(value)
|
51
|
+
when :binary then column.binary_to_string(value)
|
52
|
+
when :boolean then column.value_to_boolean(value)
|
53
|
+
else value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def columns(table_name)
|
58
|
+
@columns[table_name] ||= @schema[table_name.to_s][:columns].collect {|c| Column.new(c[0], c[1]) }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module MockedFixtures
|
2
|
+
class MockFactory
|
3
|
+
class << self
|
4
|
+
|
5
|
+
# Create mock object from fixture
|
6
|
+
def create_mock(type, model_class, fixture, testcase)
|
7
|
+
testcase.send("mock_model_with_#{type}", model_class, { :all_attributes => true, :add_errors => true }.merge(fixture) )
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module MockedFixtures
|
2
|
+
class MockFixtures < Fixtures
|
3
|
+
attr_reader :connection
|
4
|
+
|
5
|
+
# Gets loaded fixture files for each table and type casts the values and
|
6
|
+
# returns the array of MockFixtures instances. The insert_fixtures method
|
7
|
+
# is in the superclass and does all the foxy fixtures work.
|
8
|
+
def self.create_fixtures(fixtures_directory, table_names, class_names = {})
|
9
|
+
table_names = Array(table_names).flatten.map { |n| n.to_s }
|
10
|
+
fixtures = table_names.map do |table_name|
|
11
|
+
fixture = MockedFixtures::MockFixtures.new(File.split(table_name.to_s).last, class_names[table_name.to_sym], File.join(fixtures_directory, table_name.to_s))
|
12
|
+
fixture.insert_fixtures
|
13
|
+
fixture.each {|label, f| fixture[label] = fixture.connection.loaded_fixtures[fixture.table_name][label] }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
|
18
|
+
@table_name, @fixture_path, @file_filter = table_name, fixture_path, file_filter
|
19
|
+
@class_name = class_name || (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
|
20
|
+
@table_name = ActiveRecord::Base.table_name_prefix + @table_name + ActiveRecord::Base.table_name_suffix
|
21
|
+
@table_name = class_name.table_name if class_name.respond_to?(:table_name)
|
22
|
+
@connection = MockedFixtures::MockConnection.new
|
23
|
+
read_fixture_files
|
24
|
+
end
|
25
|
+
|
26
|
+
# overrides method to prevent database hit
|
27
|
+
def has_primary_key_column?
|
28
|
+
@has_primary_key_column ||= !primary_key_name.nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
# overrides method to prevent database hit and uses dummy connection
|
32
|
+
# which gets primary key from schema.rb
|
33
|
+
def primary_key_name
|
34
|
+
@primary_key_name ||= @connection.schema[@table_name.to_s][:primary_key]
|
35
|
+
end
|
36
|
+
|
37
|
+
def column_names
|
38
|
+
@column_name ||= @connection.columns(@table_name).collect(&:name)
|
39
|
+
end
|
40
|
+
|
41
|
+
def delete_existing_fixtures
|
42
|
+
# override to do nothing (NOP) as we are not using the database, so nothing needs deleting
|
43
|
+
end
|
44
|
+
|
45
|
+
# The each method is aliased to allow the fixture label to be captured during
|
46
|
+
# iteration in superclass methods, particularly the insert_fixtures method.
|
47
|
+
# The label is required for the insert_fixture connection method to be able
|
48
|
+
# store the final fixture hash and reference it by its label for later.
|
49
|
+
alias :original_each :each
|
50
|
+
def each
|
51
|
+
original_each do |label, fixture|
|
52
|
+
connection.current_fixture_label = label
|
53
|
+
yield label, fixture
|
54
|
+
connection.current_fixture_label = nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'flexmock/test_unit'
|
2
|
+
module MockedFixtures
|
3
|
+
module Mocks
|
4
|
+
module Flexmock
|
5
|
+
|
6
|
+
def mock_model_with_flexmock(model_class, options_and_stubs={})
|
7
|
+
all_attributes = options_and_stubs.delete(:all_attributes)
|
8
|
+
add_errors = options_and_stubs.delete(:add_errors)
|
9
|
+
|
10
|
+
if all_attributes
|
11
|
+
schema = MockedFixtures::SchemaParser.load_schema
|
12
|
+
table = model_class.table_name
|
13
|
+
schema[table][:columns].each { |column| options_and_stubs[column[0].to_sym] = nil unless options_and_stubs.has_key?(column[0].to_sym) }
|
14
|
+
end
|
15
|
+
if add_errors
|
16
|
+
errors = flexmock(Array.new, :count => 0, :on => nil)
|
17
|
+
options_and_stubs.reverse_merge!(:errors => errors)
|
18
|
+
end
|
19
|
+
mock_helper = FlexMock::MockContainerHelper.new
|
20
|
+
id = options_and_stubs[:id] || mock_helper.next_id
|
21
|
+
options_and_stubs.reverse_merge!({
|
22
|
+
:id => id,
|
23
|
+
:to_param => id.to_s,
|
24
|
+
})
|
25
|
+
|
26
|
+
mock = flexmock("#{model_class}_#{id}", options_and_stubs)
|
27
|
+
mock_helper.add_model_methods(mock, model_class, id)
|
28
|
+
mock
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Test::Unit::TestCase.send(:include, MockedFixtures::Mocks::Flexmock)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'mocha'
|
2
|
+
module MockedFixtures
|
3
|
+
module Mocks
|
4
|
+
module Mocha
|
5
|
+
|
6
|
+
def mock_model_with_mocha(model_class, options_and_stubs={})
|
7
|
+
all_attributes = options_and_stubs.delete(:all_attributes)
|
8
|
+
add_errors = options_and_stubs.delete(:add_errors)
|
9
|
+
if all_attributes
|
10
|
+
schema = MockedFixtures::SchemaParser.load_schema
|
11
|
+
table = model_class.table_name
|
12
|
+
schema[table][:columns].each { |column| options_and_stubs[column[0].to_sym] = nil unless options_and_stubs.has_key?(column[0].to_sym) }
|
13
|
+
end
|
14
|
+
if add_errors
|
15
|
+
errors = []
|
16
|
+
errors.stubs(:count).returns(0)
|
17
|
+
errors.stubs(:on).returns(nil)
|
18
|
+
options_and_stubs.reverse_merge!(:errors => errors)
|
19
|
+
end
|
20
|
+
options_and_stubs.reverse_merge!(
|
21
|
+
:id => options_and_stubs[:id],
|
22
|
+
:to_param => options_and_stubs[:id].to_s,
|
23
|
+
:new_record? => false
|
24
|
+
)
|
25
|
+
obj = stub("#{model_class}_#{options_and_stubs[:id]}", options_and_stubs)
|
26
|
+
obj.instance_eval <<-CODE
|
27
|
+
def is_a?(other)
|
28
|
+
#{model_class}.ancestors.include?(other)
|
29
|
+
end
|
30
|
+
def kind_of?(other)
|
31
|
+
#{model_class}.ancestors.include?(other)
|
32
|
+
end
|
33
|
+
def instance_of?(other)
|
34
|
+
other == #{model_class}
|
35
|
+
end
|
36
|
+
def class
|
37
|
+
#{model_class}
|
38
|
+
end
|
39
|
+
CODE
|
40
|
+
obj
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
Test::Unit::TestCase.send(:include, MockedFixtures::Mocks::Mocha)
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# rspec-rails mock_model extensions to add the stubs
|
2
|
+
# for all attributes and the errors method
|
3
|
+
module MockedFixtures
|
4
|
+
module Mocks
|
5
|
+
module Rspec
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.class_eval do
|
9
|
+
include InstanceMethods
|
10
|
+
alias_method_chain :mock_model, :attributes
|
11
|
+
alias_method :mock_model_with_rspec, :mock_model_with_attributes
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
def mock_model_with_attributes(model_class, options_and_stubs = {})
|
17
|
+
if options_and_stubs.delete(:all_attributes)
|
18
|
+
schema = MockedFixtures::SchemaParser.load_schema
|
19
|
+
table = model_class.table_name
|
20
|
+
schema[table][:columns].each { |column|
|
21
|
+
unless options_and_stubs.has_key?(column[0].to_sym) || column[0] == model_class.primary_key
|
22
|
+
options_and_stubs[column[0].to_sym] = nil
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
if options_and_stubs.delete(:add_errors)
|
27
|
+
errors = []
|
28
|
+
errors.stub!(:count).and_return(0)
|
29
|
+
errors.stub!(:on).and_return(nil)
|
30
|
+
options_and_stubs.reverse_merge!(:errors => errors)
|
31
|
+
end
|
32
|
+
mock_model_without_attributes(model_class, options_and_stubs)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
Spec::Rails::Example::RailsExampleGroup.send(:include, MockedFixtures::Mocks::Rspec)
|