mocked_fixtures 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/lib/mocked_fixtures.rb +1 -1
- data/lib/mocked_fixtures/testcase.rb +50 -55
- data/lib/mocked_fixtures/version.rb +1 -1
- data/spec/connection_adapters/sqlserver_adapter_spec.rb +6 -19
- data/spec/mock_fixtures_spec.rb +4 -0
- data/spec/spec_helper.rb +57 -3
- data/spec/testcase_spec.rb +28 -10
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
== 0.2.0 2008-09-04
|
2
|
+
|
3
|
+
* New features:
|
4
|
+
* Added blocks to mock fixture accessors to allow neater customisation of mock objects as they are loaded
|
5
|
+
* Refactored hooks for loading mock fixtures on test setup. Much simpler and handles Rails 2.0 and 2.1
|
6
|
+
without load order issues with rspec-rails
|
7
|
+
|
1
8
|
== 0.1.0 2008-09-03
|
2
9
|
|
3
10
|
* 1 major enhancement:
|
data/lib/mocked_fixtures.rb
CHANGED
@@ -9,7 +9,7 @@ require 'mocked_fixtures/version'
|
|
9
9
|
|
10
10
|
if defined?(Spec::Example)
|
11
11
|
require 'mocked_fixtures/spec/configuration'
|
12
|
-
Test::Unit::TestCase.
|
12
|
+
# Test::Unit::TestCase.mock_fixtures_with :rspec
|
13
13
|
end
|
14
14
|
|
15
15
|
MockedFixtures::SchemaParser.schema_path = RAILS_ROOT + '/db/schema.rb'
|
@@ -43,71 +43,38 @@ module Test
|
|
43
43
|
table_names.map { |f| File.basename(f).split('.')[0..-2].join('.') }
|
44
44
|
end
|
45
45
|
|
46
|
-
# Adds test case setup method to run before each test to load fixture
|
47
|
-
# files and setup mock fixture accessors for test class. This runs before
|
48
|
-
# each test as the test class is recreated each time.
|
49
|
-
def self.method_added_with_mock_fixtures(method)
|
50
|
-
return if @__disable_method_added__
|
51
|
-
@__disable_method_added__ = true
|
52
|
-
|
53
|
-
if method.to_s == 'setup'
|
54
|
-
unless method_defined?(:setup_without_mock_fixtures)
|
55
|
-
alias_method :setup_without_mock_fixtures, :setup
|
56
|
-
define_method(:mock_fixture_setup) do
|
57
|
-
setup_with_mock_fixtures
|
58
|
-
setup_without_mock_fixtures
|
59
|
-
end
|
60
|
-
end
|
61
|
-
alias_method :setup, :mock_fixture_setup
|
62
|
-
end
|
63
|
-
|
64
|
-
@__disable_method_added__ = false
|
65
|
-
method_added_without_mock_fixtures(method)
|
66
|
-
end
|
67
|
-
|
68
|
-
class << self
|
69
|
-
alias_method_chain :method_added, :mock_fixtures
|
70
|
-
end
|
71
|
-
|
72
46
|
# This creates the fixture accessors and retrieves the fixture and creates
|
73
47
|
# mock object from it. Mock fixture is then cached.
|
74
48
|
def self.setup_mock_fixture_accessors(table_names = nil)
|
75
49
|
(table_names || mock_fixture_table_names).each do |table_name|
|
76
50
|
table_name = table_name.to_s.tr('.', '_')
|
77
|
-
|
78
|
-
define_method('mock_' + table_name) do |*fixtures|
|
79
|
-
@mock_fixture_cache ||= {}
|
80
|
-
@mock_fixture_cache[table_name] ||= {}
|
81
|
-
if fixtures.first == :all
|
82
|
-
fixtures = self.class.loaded_mock_fixtures[table_name].keys
|
83
|
-
end
|
84
|
-
|
85
|
-
mock_type = self.class.mocked_fixtures_mock_framework
|
86
|
-
|
87
|
-
instances = fixtures.map do |fixture_name|
|
88
|
-
if fixture = self.class.loaded_mock_fixtures[table_name][fixture_name.to_s]
|
89
|
-
unless model_class = self.class.loaded_mock_fixtures[table_name].send(:model_class)
|
90
|
-
raise StandardError, "No model class found for table name '#{table_name}'. Specify it explicitly 'set_fixture_class :table_name => 'ClassName'."
|
91
|
-
end
|
92
|
-
|
93
|
-
@mock_fixture_cache[table_name][fixture_name] ||= MockedFixtures::MockFactory.create_mock(mock_type, model_class, fixture, self)
|
94
|
-
else
|
95
|
-
raise StandardError, "No fixture named '#{fixture_name}' was found for table '#{table_name}'"
|
96
|
-
end
|
97
|
-
end
|
98
51
|
|
99
|
-
|
100
|
-
|
101
|
-
|
52
|
+
class_eval <<-END
|
53
|
+
def mock_#{table_name}(*fixtures, &block)
|
54
|
+
create_mock_fixtures('#{table_name}', *fixtures, &block)
|
55
|
+
end
|
56
|
+
END
|
102
57
|
end
|
103
|
-
end
|
104
|
-
|
58
|
+
end
|
59
|
+
|
105
60
|
def setup_with_mock_fixtures
|
106
61
|
fixtures_to_load = self.class.mock_fixture_table_names - self.class.loaded_mock_fixtures.keys
|
107
|
-
|
108
|
-
|
62
|
+
load_mock_fixtures(fixtures_to_load) unless fixtures_to_load.empty?
|
63
|
+
setup_without_mock_fixtures
|
64
|
+
end
|
65
|
+
|
66
|
+
# Aliasing test setup method to get mock fixtures loaded.
|
67
|
+
#
|
68
|
+
# If app is Rails 2.0.x and rspec-rails is loaded before plugin,
|
69
|
+
# when used as gem perhaps, it prevents aliasing setup method,
|
70
|
+
if method_defined?(:setup_with_fixtures)
|
71
|
+
alias_method :setup_without_mock_fixtures, :setup_with_fixtures
|
72
|
+
alias_method :setup_with_fixtures, :setup_with_mock_fixtures
|
73
|
+
else
|
74
|
+
alias_method_chain :setup, :mock_fixtures
|
109
75
|
end
|
110
|
-
|
76
|
+
|
77
|
+
private
|
111
78
|
|
112
79
|
# Loads fixtures to be mocked and cache them in class variable
|
113
80
|
def load_mock_fixtures(fixtures_to_load)
|
@@ -116,6 +83,34 @@ module Test
|
|
116
83
|
fixtures.each { |f| self.class.loaded_mock_fixtures[f.table_name] = f }
|
117
84
|
end
|
118
85
|
end
|
86
|
+
|
87
|
+
# Create mock fixture objects for fixture names and cache. Pass each
|
88
|
+
# fixture to block, if given, to customize mock object for local use.
|
89
|
+
def create_mock_fixtures(table_name, *fixtures, &block)
|
90
|
+
@mock_fixture_cache ||= {}
|
91
|
+
@mock_fixture_cache[table_name] ||= {}
|
92
|
+
if fixtures.first == :all
|
93
|
+
fixtures = self.class.loaded_mock_fixtures[table_name].keys
|
94
|
+
end
|
95
|
+
|
96
|
+
mock_type = self.class.mocked_fixtures_mock_framework
|
97
|
+
|
98
|
+
instances = fixtures.map do |fixture_name|
|
99
|
+
if fixture = self.class.loaded_mock_fixtures[table_name][fixture_name.to_s]
|
100
|
+
unless model_class = self.class.loaded_mock_fixtures[table_name].send(:model_class)
|
101
|
+
raise StandardError, "No model class found for table name '#{table_name}'. Specify it explicitly 'set_fixture_class :table_name => 'ClassName'."
|
102
|
+
end
|
103
|
+
mock_fixture = MockedFixtures::MockFactory.create_mock(mock_type, model_class, fixture, self)
|
104
|
+
@mock_fixture_cache[table_name][fixture_name] ||= mock_fixture
|
105
|
+
yield(mock_fixture) if block_given?
|
106
|
+
mock_fixture
|
107
|
+
else
|
108
|
+
raise StandardError, "No fixture named '#{fixture_name}' was found for table '#{table_name}'"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
instances.size == 1 ? instances.first : instances
|
113
|
+
end
|
119
114
|
|
120
115
|
end
|
121
116
|
end
|
@@ -2,33 +2,18 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
|
2
2
|
|
3
3
|
begin
|
4
4
|
gem 'activerecord-sqlserver-adapter'
|
5
|
+
require 'active_record/connection_adapters/sqlserver_adapter'
|
5
6
|
require 'mocked_fixtures/connection_adapters/sqlserver_adapter'
|
6
|
-
rescue LoadError
|
7
|
+
rescue LoadError, StandardError
|
7
8
|
puts 'Testing ActiveRecord SQLServer Adapter skipped'
|
8
9
|
end
|
9
10
|
|
10
|
-
if defined?(
|
11
|
-
conn = {
|
12
|
-
:adapter => 'sqlserver',
|
13
|
-
:host => 'localhost',
|
14
|
-
:mode => 'odbc',
|
15
|
-
:dsn => 'activerecord_unittest',
|
16
|
-
:database => 'activerecord_unittest',
|
17
|
-
:username => 'rails',
|
18
|
-
:password => nil
|
19
|
-
}
|
20
|
-
|
21
|
-
ActiveRecord::Base.establish_connection(conn)
|
11
|
+
if defined?(ActiveRecord::ConnectionAdapters::SQLServerAdapter)
|
22
12
|
|
23
13
|
ActiveRecord::ConnectionAdapters::SQLServerAdapter.send(:include, MockedFixtures::ConnectionAdapters::SQLServerAdapter)
|
24
14
|
|
25
|
-
require 'resources/schema.rb'
|
26
|
-
|
27
15
|
describe MockedFixtures::ConnectionAdapters::SQLServerAdapter do
|
28
|
-
|
29
|
-
before do
|
30
|
-
ActiveRecord::Base.connection.reconnect! unless ActiveRecord::Base.connection.active?
|
31
|
-
end
|
16
|
+
connect_db('mssql')
|
32
17
|
|
33
18
|
it "should allow SchemDumper to dump primary key option for pk other than 'id'" do
|
34
19
|
schema = StringIO.new
|
@@ -40,5 +25,7 @@ if defined?(MockedFixtures::ConnectionAdapters::SQLServerAdapter)
|
|
40
25
|
it "should return primary key and sequence" do
|
41
26
|
ActiveRecord::Base.connection.pk_and_sequence_for('companies').should == ['cid', nil]
|
42
27
|
end
|
28
|
+
|
29
|
+
disconnect_db
|
43
30
|
end
|
44
31
|
end
|
data/spec/mock_fixtures_spec.rb
CHANGED
@@ -7,6 +7,10 @@ describe MockedFixtures::MockFixtures do
|
|
7
7
|
ActiveRecord::Base.connection.disconnect! rescue nil
|
8
8
|
end
|
9
9
|
|
10
|
+
after(:all) do
|
11
|
+
ActiveRecord::Base.connection.reconnect! rescue nil
|
12
|
+
end
|
13
|
+
|
10
14
|
before(:each) do
|
11
15
|
@fixture_path = Test::Unit::TestCase.fixture_path
|
12
16
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,14 +4,26 @@ $:.unshift File.dirname(__FILE__)
|
|
4
4
|
require 'rubygems'
|
5
5
|
require 'spec'
|
6
6
|
|
7
|
+
vendored_rails = File.dirname(__FILE__) + '/../../../../vendor/rails'
|
8
|
+
|
9
|
+
if vendored = File.exists?(vendored_rails)
|
10
|
+
Dir.glob(vendored_rails + "/**/lib").each { |dir| $:.unshift dir }
|
11
|
+
else
|
12
|
+
gem 'rails', "=#{ENV['VERSION']}" if ENV['VERSION']
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'rails/version'
|
16
|
+
require 'active_support'
|
7
17
|
require 'active_record'
|
8
18
|
require 'active_record/fixtures'
|
9
|
-
require '
|
19
|
+
require 'active_record/version'
|
20
|
+
|
21
|
+
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{Rails::VERSION::STRING} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
|
10
22
|
|
11
23
|
require 'rspec-rails/rspec-rails'
|
12
24
|
|
13
|
-
require 'mocked_fixtures/schema_parser'
|
14
25
|
require 'mocked_fixtures/testcase'
|
26
|
+
require 'mocked_fixtures/schema_parser'
|
15
27
|
require 'mocked_fixtures/mock_fixtures'
|
16
28
|
require 'mocked_fixtures/mock_factory'
|
17
29
|
require 'mocked_fixtures/mock_connection'
|
@@ -19,9 +31,51 @@ require 'mocked_fixtures/mock_connection'
|
|
19
31
|
require 'resources/company'
|
20
32
|
require 'resources/employee'
|
21
33
|
|
22
|
-
|
23
34
|
MockedFixtures::SchemaParser.schema_path = File.expand_path(File.dirname(__FILE__) + '/resources/schema.rb')
|
24
35
|
|
25
36
|
Test::Unit::TestCase.fixture_path = File.expand_path(File.dirname(__FILE__) + '/resources')
|
26
37
|
|
38
|
+
ActiveRecord::Migration.verbose = false
|
39
|
+
|
40
|
+
ActiveRecord::Base.configurations['sqlite'] = {
|
41
|
+
:adapter => 'sqlite3',
|
42
|
+
:database => ':memory:'
|
43
|
+
}
|
44
|
+
|
45
|
+
ActiveRecord::Base.configurations['mssql'] = {
|
46
|
+
:adapter => 'sqlserver',
|
47
|
+
:host => 'localhost',
|
48
|
+
:mode => 'odbc',
|
49
|
+
:dsn => 'activerecord_unittest',
|
50
|
+
:database => 'activerecord_unittest',
|
51
|
+
:username => 'rails',
|
52
|
+
:password => nil
|
53
|
+
}
|
54
|
+
|
55
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['sqlite'])
|
56
|
+
|
57
|
+
module SpecHelpers
|
58
|
+
def self.included(base)
|
59
|
+
base.extend ClassMethods
|
60
|
+
end
|
61
|
+
|
62
|
+
module ClassMethods
|
63
|
+
def connect_db(config)
|
64
|
+
before(:all) do
|
65
|
+
ActiveRecord::Base.connection.disconnect! rescue nil
|
66
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[config])
|
67
|
+
require 'resources/schema'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def disconnect_db
|
72
|
+
after(:all) do
|
73
|
+
ActiveRecord::Base.connection.disconnect! rescue nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
27
78
|
|
79
|
+
Spec::Runner.configure do |config|
|
80
|
+
config.include SpecHelpers
|
81
|
+
end
|
data/spec/testcase_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
describe Test::Unit::TestCase, "extended with mocked_fixtures" do
|
3
|
+
describe 'Test::Unit::TestCase', "when extended with mocked_fixtures" do
|
4
4
|
|
5
5
|
def klass
|
6
6
|
Test::Unit::TestCase
|
@@ -24,7 +24,7 @@ describe Test::Unit::TestCase, "extended with mocked_fixtures" do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should return all fixture table names" do
|
27
|
-
klass.all_fixture_table_names.should
|
27
|
+
klass.all_fixture_table_names.should include('employees', 'companies')
|
28
28
|
end
|
29
29
|
|
30
30
|
describe "global mock fixtures" do
|
@@ -36,11 +36,11 @@ describe Test::Unit::TestCase, "extended with mocked_fixtures" do
|
|
36
36
|
|
37
37
|
it "should include all fixtures if equals :all" do
|
38
38
|
klass.global_mock_fixtures = :all
|
39
|
-
klass.mock_fixture_table_names.should
|
39
|
+
klass.mock_fixture_table_names.should include('employees', 'companies')
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
describe "mocked fixture
|
43
|
+
describe "mocked fixture accessors" do
|
44
44
|
mock_fixtures :companies, :employees
|
45
45
|
|
46
46
|
before(:all) do
|
@@ -63,11 +63,29 @@ describe Test::Unit::TestCase, "extended with mocked_fixtures" do
|
|
63
63
|
lambda { mock_companies(:non_fixture) }.should raise_error(StandardError, /No fixture named 'non_fixture'/)
|
64
64
|
end
|
65
65
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
66
|
+
it "should call block on mock object requested" do
|
67
|
+
employee = mock_employees(:adam) do |e|
|
68
|
+
e.stub!(:last_name).and_return('MEEHAN')
|
69
|
+
end
|
70
|
+
employee.last_name.should == "MEEHAN"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should call block on all mock objects requested" do
|
74
|
+
employees = mock_employees(:adam, :jane) do |e|
|
75
|
+
e.stub!(:last_name).and_return('same')
|
76
|
+
end
|
77
|
+
employees.all? {|e| e.last_name.should == "same" }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "regular fixtures" do
|
82
|
+
fixtures :companies
|
83
|
+
|
84
|
+
connect_db('sqlite')
|
85
|
+
disconnect_db
|
86
|
+
|
87
|
+
it "should be loaded" do
|
88
|
+
companies(:mega_corp).should_not be_nil
|
89
|
+
end
|
72
90
|
end
|
73
91
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mocked_fixtures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-09-
|
12
|
+
date: 2008-09-04 00:00:00 +10:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|