rspec-active_record_mocks 0.0.2 → 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.
- checksums.yaml +4 -4
 - data/Readme.md +15 -1
 - data/lib/rspec/active_record_mocks.rb +46 -6
 - data/lib/rspec/active_record_mocks/version.rb +1 -1
 - metadata +15 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 1c835e322ee4846866ac0aaea6a04b020d2def5f
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 3520cefddbf2fb248a76714f9b5de788de9394eb
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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  
     | 
| 
      
 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 =  
     | 
| 
      
 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 
     | 
    
         
            -
                       
     | 
| 
       27 
     | 
    
         
            -
                       
     | 
| 
      
 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  
     | 
| 
      
 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 
     | 
    
         | 
    
        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 
     | 
| 
      
 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
         
     |