rspec-active_record_mocks 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +14 -0
- data/License +11 -0
- data/Rakefile +5 -0
- data/Readme.md +92 -0
- data/lib/rspec/active_record_mocks/version.rb +5 -0
- data/lib/rspec/active_record_mocks.rb +75 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e7d77ac47d5cbc287abb32c9174b72ab2e57d5d7
|
4
|
+
data.tar.gz: 858bfe8e0a51048aa981c2eb4e01136f6adb6f84
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d94b117f61a69f4b85d4a6ad6f894bb500d5264e56e134f0e967fbf5b79a520e16d1bc6842e09180d9ca02f9eece02dd08f76bccadddc4c8f672848ba8a9d1d
|
7
|
+
data.tar.gz: ad982662ca497971c46d27cbca472ab1e526768e91cac818451e70333d46bd11f8d795fc93564fc6265239c7f5e340a6d8f4da838c2fceaa7693b5ff40ad8594
|
data/Gemfile
ADDED
data/License
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
Copyright 2013 Jordon Bedwell
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
4
|
+
use this file except in compliance with the License. You may obtain a copy of
|
5
|
+
the License at: http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
9
|
+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
10
|
+
License for the specific language governing permissions and limitations under
|
11
|
+
the License.
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# RSpec Active Record Mocks.
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/envygeeks/rspec-active_record_mocks.png?branch=master)](https://travis-ci.org/envygeeks/rspec-active_record_mocks) [![Coverage Status](https://coveralls.io/repos/envygeeks/rspec-active_record_mocks/badge.png?branch=master)](https://coveralls.io/r/envygeeks/rspec-active_record_mocks) [![Code Climate](https://codeclimate.com/github/envygeeks/rspec-active_record_mocks.png)](https://codeclimate.com/github/envygeeks/rspec-active_record_mocks) [![Dependency Status](https://gemnasium.com/envygeeks/rspec-active_record_mocks.png)](https://gemnasium.com/envygeeks/rspec-active_record_mocks)
|
4
|
+
|
5
|
+
ActiveRecord Mocks is designed to aide you in testing your ActiveRecord
|
6
|
+
concerns by creating random models (or even named models) that are removed
|
7
|
+
after each test.
|
8
|
+
|
9
|
+
## Installing
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "rspec-active_record_mocks"
|
13
|
+
```
|
14
|
+
|
15
|
+
## Using
|
16
|
+
|
17
|
+
`RSpec::ActiveRecordMocks` supports `before` with `:all` or `:each`, it can
|
18
|
+
also be used directly inside the it. It's designed to try and be a little
|
19
|
+
bit flexible in how you try to use it.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
# ----------------------------------------------------------------------------
|
24
|
+
# One Line Usage.
|
25
|
+
# ----------------------------------------------------------------------------
|
26
|
+
|
27
|
+
describe TestConcern do
|
28
|
+
it 'should work as expected' do
|
29
|
+
expect(mock_active_record_model(:include => TestConcern).test_column).to eq "value"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
|
36
|
+
# ----------------------------------------------------------------------------
|
37
|
+
# Before :all example with :include and a migration.
|
38
|
+
# Also works with `before :each`
|
39
|
+
# ----------------------------------------------------------------------------
|
40
|
+
|
41
|
+
describe TestConcern do
|
42
|
+
before :all do
|
43
|
+
@test_model = mock_active_record_model(:include => TestConcern) do
|
44
|
+
table.string(:test_column)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have the concerns method" do
|
49
|
+
expect(@test_model.new).to respond_to :test_method
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
|
56
|
+
# ----------------------------------------------------------------------------
|
57
|
+
# Before :all example that does a class_eval to include.
|
58
|
+
# Also works with `before :each`
|
59
|
+
# ----------------------------------------------------------------------------
|
60
|
+
|
61
|
+
describe TestConcern do
|
62
|
+
before :all do
|
63
|
+
@model = mock_active_record_model.class_eval do
|
64
|
+
include MyActiveRecordConcern
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should work" do
|
69
|
+
expect(@model.concern_method).to eq "concern_value"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
|
76
|
+
# ----------------------------------------------------------------------------
|
77
|
+
# Completely random example in a before :all.
|
78
|
+
# Also works with `before :each`
|
79
|
+
# ----------------------------------------------------------------------------
|
80
|
+
|
81
|
+
describe MyActiveRecordConcern do
|
82
|
+
before :all do
|
83
|
+
@model = mock_active_record_model do |table|
|
84
|
+
table.string(:column)
|
85
|
+
end
|
86
|
+
|
87
|
+
@model.class_eval do
|
88
|
+
include MyActiveRecordConcern
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require "active_support/core_ext/string/inflections"
|
2
|
+
require "active_record"
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
module ActiveRecordMocks
|
6
|
+
|
7
|
+
# ------------------------------------------------------------------------
|
8
|
+
# Allow people to mock ActiveRecord while giving them the flexibility.
|
9
|
+
# ------------------------------------------------------------------------
|
10
|
+
|
11
|
+
def mock_active_record_model(opts = {}, &block)
|
12
|
+
tbl = create_active_record_table_for_mocking(opts.delete(:name), &block)
|
13
|
+
Object.const_set(tbl.camelize, Class.new(ActiveRecord::Base)).class_eval do
|
14
|
+
include opts.delete(:include) if opts[:include]
|
15
|
+
self.table_name = tbl and self
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# ------------------------------------------------------------------------
|
20
|
+
# Roll through each one of the created tables and destroy them.
|
21
|
+
# ------------------------------------------------------------------------
|
22
|
+
|
23
|
+
def clean_tables_for_active_record_mocking
|
24
|
+
if mocked_active_record_options[:mocked_active_record_tables]
|
25
|
+
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)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def mocked_active_record_options
|
34
|
+
(example.nil?) ? (@mocked_active_record_options ||= {}) : example.options
|
35
|
+
end
|
36
|
+
|
37
|
+
# ------------------------------------------------------------------------
|
38
|
+
# Creates a temporary table inside of the database using ActiveRecord.
|
39
|
+
# ------------------------------------------------------------------------
|
40
|
+
|
41
|
+
private
|
42
|
+
def create_active_record_table_for_mocking(tbl = nil, &block)
|
43
|
+
tbl = (tbl || SecureRandom.hex(30).tr('^a-z', '')).to_s
|
44
|
+
setup_active_record_mocking_table(tbl, &block)
|
45
|
+
tbl
|
46
|
+
end
|
47
|
+
|
48
|
+
# ------------------------------------------------------------------------
|
49
|
+
# Sets up the table using an ActiveRecord migration.
|
50
|
+
# ------------------------------------------------------------------------
|
51
|
+
|
52
|
+
private
|
53
|
+
def setup_active_record_mocking_table(tbl, &block)
|
54
|
+
(mocked_active_record_options[:mocked_active_record_tables] ||= []).push(tbl)
|
55
|
+
ActiveRecord::Migration.suppress_messages do
|
56
|
+
ActiveRecord::Migration.create_table tbl do |obj|
|
57
|
+
block.call(obj) if block_given?
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# ----------------------------------------------------------------------------
|
65
|
+
# Add ourself to the win list so they can use the methods.
|
66
|
+
# ----------------------------------------------------------------------------
|
67
|
+
|
68
|
+
RSpec.configure do |config|
|
69
|
+
config.include RSpec::ActiveRecordMocks
|
70
|
+
[:all, :each].each do |type|
|
71
|
+
config.after(type) do
|
72
|
+
clean_tables_for_active_record_mocking
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-active_record_mocks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordon Bedwell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.14'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
- - <=
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '4.0'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.2'
|
44
|
+
- - <=
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '4.0'
|
47
|
+
description: Mock ActiveRecord tables to test concerns and other code.
|
48
|
+
email: envygeeks@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- Readme.md
|
54
|
+
- License
|
55
|
+
- Rakefile
|
56
|
+
- Gemfile
|
57
|
+
- lib/rspec/active_record_mocks/version.rb
|
58
|
+
- lib/rspec/active_record_mocks.rb
|
59
|
+
homepage: https://github.com/envygeeks/rspec-active_record_mocks
|
60
|
+
licenses:
|
61
|
+
- Apache 2.0
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Mock ActiveRecord tables to test.
|
83
|
+
test_files: []
|