rspec-active_record_mocks 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c42a255ef423c6394b1b4a341bf4008eda04b89
4
- data.tar.gz: a4ca9cd166d4814114f6acca76dec36f2efb22de
3
+ metadata.gz: 1c835e322ee4846866ac0aaea6a04b020d2def5f
4
+ data.tar.gz: 3520cefddbf2fb248a76714f9b5de788de9394eb
5
5
  SHA512:
6
- metadata.gz: 3f94aef73afbe5b7350589061191d8b663f1f4c53910c43533d94ac04ba795fd768b239c4e3ccec802c9a389f40c5736c71f9bca1a2ae843eed6e8895d7b59f2
7
- data.tar.gz: d29c123ffbc3be125e08b54e19d92371a21b0805af7c00345ab25d8dc208e3573842f8a33fdd5056b459475b5a0244b5979126bd16e4e014f0ebc9711e617a39
6
+ metadata.gz: 98d2e2d634e42f2ab73a0f91bc04798d527f8549f1b4f5e458ec28d184e1402079f5ffc8d2cef660749c554b002fd5aae475eba57fb0612021de8d2d2ed7775c
7
+ data.tar.gz: 50085a2dbfbe5ffff4d928da02c10ad718ef751461e9c7cb21c20a9d34a714a41ceda09727d5e4457c8cf6e983097e03f88e0473c73f49fd2480e9b6b6125264
data/Readme.md CHANGED
@@ -25,7 +25,7 @@ bit flexible in how you try to use it.
25
25
  # ----------------------------------------------------------------------------
26
26
 
27
27
  describe TestConcern do
28
- it 'should work as expected' do
28
+ it "should work as expected" do
29
29
  expect(mock_active_record_model(:include => TestConcern).test_column).to eq "value"
30
30
  end
31
31
  end
@@ -33,6 +33,20 @@ end
33
33
 
34
34
  ```ruby
35
35
 
36
+ # ----------------------------------------------------------------------------
37
+ # An example using extensions!
38
+ # ----------------------------------------------------------------------------
39
+
40
+ describe TestConcern do
41
+ it "should work with extensions" do
42
+ mock_active_record_model(:extensions => :hstore)
43
+ expect(ActiveRecord::Base.connection.extensions).to include "hstore"
44
+ end
45
+ end
46
+ ```
47
+
48
+ ```ruby
49
+
36
50
  # ----------------------------------------------------------------------------
37
51
  # Before :all example with :include and a migration.
38
52
  # Also works with `before :each`
@@ -3,13 +3,19 @@ require "active_record"
3
3
 
4
4
  module RSpec
5
5
  module ActiveRecordMocks
6
+ class ExtensionsUnsupportedError < StandardError
7
+ def initialize
8
+ super "Your adapter does not support PostgreSQL extensions"
9
+ end
10
+ end
6
11
 
7
12
  # ------------------------------------------------------------------------
8
13
  # Allow people to mock ActiveRecord while giving them the flexibility.
9
14
  # ------------------------------------------------------------------------
10
15
 
11
16
  def mock_active_record_model(opts = {}, &block)
12
- tbl = create_active_record_table_for_mocking(opts.delete(:name), &block)
17
+ tbl, ext = opts.delete(:name), opts.delete(:extensions)
18
+ tbl = create_active_record_table_for_mocking(tbl, ext, &block)
13
19
  Object.const_set(tbl.camelize, Class.new(ActiveRecord::Base)).class_eval do
14
20
  include opts.delete(:include) if opts[:include]
15
21
  self.table_name = tbl and self
@@ -23,12 +29,29 @@ module RSpec
23
29
  def clean_tables_for_active_record_mocking
24
30
  if mocked_active_record_options[:mocked_active_record_tables]
25
31
  mocked_active_record_options.delete(:mocked_active_record_tables).each do |tbl|
26
- Object.send(:remove_const, tbl.camelize)
27
- ActiveRecord::Base.connection.drop_table(tbl)
32
+ ActiveRecord::Base.connection.drop_table(tbl) if active_record_tables.include?(tbl)
33
+ if Object.const_defined?(tbl.camelize)
34
+ Object.send(:remove_const, tbl.camelize)
35
+ end
28
36
  end
29
37
  end
30
38
  end
31
39
 
40
+ # ------------------------------------------------------------------------
41
+ # Aliases ActiveRecord::Base.connection.tables to active_record_tables to
42
+ # a geniunely useful method that can be used by anybody doing db testing.
43
+ # ------------------------------------------------------------------------
44
+
45
+ def active_record_tables
46
+ ActiveRecord::Base.connection.tables
47
+ end
48
+
49
+ # ------------------------------------------------------------------------
50
+ # Allows us to access options for either the class or the test itself as
51
+ # to allow users to either work on the class or work in the test allowing
52
+ # us to cleanup without affecting the other.
53
+ # ------------------------------------------------------------------------
54
+
32
55
  private
33
56
  def mocked_active_record_options
34
57
  (example.nil?) ? (@mocked_active_record_options ||= {}) : example.options
@@ -39,9 +62,9 @@ module RSpec
39
62
  # ------------------------------------------------------------------------
40
63
 
41
64
  private
42
- def create_active_record_table_for_mocking(tbl = nil, &block)
65
+ def create_active_record_table_for_mocking(tbl, ext, &block)
43
66
  tbl = (tbl || SecureRandom.hex(30).tr('^a-z', '')).to_s
44
- setup_active_record_mocking_table(tbl, &block)
67
+ setup_active_record_mocking_table(tbl, ext, &block)
45
68
  tbl
46
69
  end
47
70
 
@@ -50,14 +73,31 @@ module RSpec
50
73
  # ------------------------------------------------------------------------
51
74
 
52
75
  private
53
- def setup_active_record_mocking_table(tbl, &block)
76
+ def setup_active_record_mocking_table(tbl, ext, &block)
54
77
  (mocked_active_record_options[:mocked_active_record_tables] ||= []).push(tbl)
55
78
  ActiveRecord::Migration.suppress_messages do
79
+ setup_active_record_mocking_extensions(ext)
56
80
  ActiveRecord::Migration.create_table tbl do |obj|
57
81
  block.call(obj) if block_given?
58
82
  end
59
83
  end
60
84
  end
85
+
86
+ # ------------------------------------------------------------------------
87
+ # Sets up the extensions for PostgreSQL.
88
+ # ------------------------------------------------------------------------
89
+
90
+ private
91
+ def setup_active_record_mocking_extensions(ext)
92
+ ext = [ext].delete_if { |value| value.blank? }.flatten
93
+ if ext.size > 0 && !ActiveRecord::Base.connection.respond_to?(:enable_extension)
94
+ raise ExtensionsUnsupportedError
95
+ else
96
+ ext.each do |extension|
97
+ ActiveRecord::Migration.enable_extension(extension)
98
+ end
99
+ end
100
+ end
61
101
  end
62
102
  end
63
103
 
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module ActiveRecordMocks
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-active_record_mocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordon Bedwell
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2013-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-expect_error
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.3
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement