transactional_capybara 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in transactional_capybara.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ian Young
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,108 @@
1
+ # Database Transactions πŸ’œ Capybara
2
+
3
+ You want your specs to use transactions for speed 🐎🐎🐎.
4
+
5
+ But as soon as you try it with Capybara, things go wrong πŸ’»πŸ’₯.
6
+
7
+ Don't flip tables.
8
+ Use this instead.
9
+
10
+ ## Setup ##
11
+
12
+ Add it to your Gemfile, of course:
13
+
14
+ gem 'transactional_capybara'
15
+
16
+ And then initialize it in your tests…
17
+
18
+ ### RSpec ###
19
+
20
+ In `rails_helper.rb` (or `spec_helper.rb`):
21
+
22
+ ```ruby
23
+ require "transactional_capybara/rspec"
24
+ ```
25
+
26
+ Your database connection is automatically shared between threads, and all specs tagged with `js: true` will wait for AJAX requests to finish before continuing.
27
+
28
+ Wow. Much convenience. So relax.
29
+
30
+ ### All other test frameworks ###
31
+
32
+ Somewhere near the beginning of your test initialization:
33
+
34
+ ```ruby
35
+ TransactionalCapybara.share_connection
36
+ ```
37
+
38
+ And then make sure to define a hook that will run after each Capybara test:
39
+
40
+ ```ruby
41
+ after :each do
42
+ TransactionalCapybara::AjaxHelpers.wait_for_ajax(page)
43
+ end
44
+ ```
45
+
46
+ ## DatabaseCleaner ##
47
+
48
+ For this gem to be able to help with AJAX, it needs to be invoked *before* DatabaseCleaner rolls back the transaction.
49
+
50
+ You should be good to go if your setup looks like this:
51
+
52
+ ```ruby
53
+ config.around(:each) do |example|
54
+ DatabaseCleaner.cleaning do
55
+ example.run
56
+ end
57
+ end
58
+ ```
59
+
60
+ But if you're using before/after hooks to clean, like:
61
+
62
+ ```ruby
63
+ before :each do
64
+ DatabaseCleaner.start
65
+ end
66
+
67
+ after :each do
68
+ DatabaseCleaner.clean
69
+ end
70
+ ```
71
+
72
+ Then you need to make sure the AJAX hook runs first.
73
+ Declare it before the DatabaseCleaner code and you should be set:
74
+
75
+ ```ruby
76
+ after :each do
77
+ TransactionalCapybara::AjaxHelpers.wait_for_ajax(page)
78
+ end
79
+
80
+ before :each do
81
+ DatabaseCleaner.start
82
+ end
83
+
84
+ after :each do
85
+ DatabaseCleaner.clean
86
+ end
87
+ ```
88
+
89
+
90
+ ## Support ##
91
+
92
+ Right now this gem automatically fixes the following things:
93
+
94
+ * ActiveRecord
95
+ * jQuery
96
+ * Angular
97
+
98
+ Don't see something you want?
99
+ I'd love a pull request, or even just a friendly inquiry!
100
+
101
+ ## Contributing
102
+
103
+ 1. Fork it
104
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
105
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
106
+ 4. Push to the branch (`git push origin my-new-feature`)
107
+ 5. Think about how I'm a bad person for not writing any tests yet
108
+ 6. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ require "transactional_capybara/version"
2
+ require "transactional_capybara/ajax_helpers"
3
+ require "transactional_capybara/shared_connection"
@@ -0,0 +1,73 @@
1
+ module TransactionalCapybara
2
+ module AjaxHelpers
3
+ class PageWaiting
4
+ def initialize(page)
5
+ @page = page
6
+ end
7
+
8
+ def wait_for_ajax
9
+ wait_until { finished_all_ajax_requests? }
10
+ end
11
+
12
+ def finished_all_ajax_requests?
13
+ (
14
+ angular_requests
15
+ + jquery_requests
16
+ ).zero?
17
+ end
18
+
19
+ def wait_until(timeout=Capybara.default_wait_time)
20
+ Timeout.timeout(timeout) do
21
+ until yield
22
+ sleep(0.01)
23
+ end
24
+ end
25
+ end
26
+
27
+ private
28
+ def run_js(expr)
29
+ @page.execute_script(expr)
30
+ end
31
+
32
+ def angular?
33
+ run_js("!!window.angular") && run_js("angular.element('[ng-app]').length") > 0
34
+ end
35
+
36
+ def angular_requests
37
+ if angular?
38
+ run_js("angular.element('[ng-app]').injector().get('$http').pendingRequests.length")
39
+ else
40
+ 0
41
+ end
42
+ end
43
+
44
+ def jquery?
45
+ run_js("!!window.jQuery")
46
+ end
47
+
48
+ def jquery_requests
49
+ if jquery?
50
+ run_js("jQuery.active")
51
+ else
52
+ 0
53
+ end
54
+ end
55
+ end
56
+
57
+ def wait_for_ajax
58
+ TransactionalCapybara::AjaxHelpers.wait_for_ajax(page)
59
+ end
60
+
61
+ def self.wait_for_ajax(page)
62
+ PageWaiting.new(page).wait_for_ajax
63
+ end
64
+
65
+ def ajax_finished?
66
+ TransactionalCapybara::AjaxHelpers.ajax_finished?(page)
67
+ end
68
+
69
+ def self.ajax_finished?(page)
70
+ PageWaiting.new(page).finished_all_ajax_requests?
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,6 @@
1
+ RSpec.configure do |config|
2
+ TransactionalCapybara.share_connection
3
+ config.after(:each, js: true) do
4
+ TransactionalCapybara::AjaxHelpers.wait_for_ajax(page)
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ class ActiveRecord::Base
2
+ mattr_accessor :shared_connection
3
+ @@shared_connection = nil
4
+
5
+ def self.connection
6
+ @@shared_connection || retrieve_connection
7
+ end
8
+ end
9
+
10
+ module TransactionalCapybara
11
+ module_function
12
+ def share_connection
13
+ ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module TransactionalCapybara
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'transactional_capybara/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transactional_capybara"
8
+ spec.version = TransactionalCapybara::VERSION
9
+ spec.authors = ["Ian Young"]
10
+ spec.email = ["ian@iangreenleaf.com"]
11
+ spec.description = %q{Support for DB transactions with Capybara}
12
+ spec.summary = %q{Speed up your test suite with database transactions, without losing your mind to Capybara connection problems. Supports shared connections, plus AJAX watchers to avoid common deadlock scenarios.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transactional_capybara
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ian Young
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-08-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Support for DB transactions with Capybara
47
+ email:
48
+ - ian@iangreenleaf.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/transactional_capybara.rb
59
+ - lib/transactional_capybara/ajax_helpers.rb
60
+ - lib/transactional_capybara/rspec.rb
61
+ - lib/transactional_capybara/shared_connection.rb
62
+ - lib/transactional_capybara/version.rb
63
+ - transactional_capybara.gemspec
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Speed up your test suite with database transactions, without losing your
89
+ mind to Capybara connection problems. Supports shared connections, plus AJAX watchers
90
+ to avoid common deadlock scenarios.
91
+ test_files: []
92
+ has_rdoc: