test_assistant 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7c14c4c6d990513adb024b4571090c7fc85dca59
4
+ data.tar.gz: 1ab16e504b25feca6a2d2ab443684d4512e4a67e
5
+ SHA512:
6
+ metadata.gz: 8361c96e295b176daf5c9aba592217be43174ba39876094fc5f7423e9f2ddfe14db46fc8e7b424b8ede85c8035c8f5b78074222f10a6be8631aea60a74e59577
7
+ data.tar.gz: 33107a1cc37d9ffd6d645b02206699c0d8f6afdb392fbf76b1e2a72cbc1fb7d0efa49294c010f58a6a8da415b72962bb72d6f9a74a5126ceff09c95fb30c2fcf
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Aleck Greenham
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.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # TestAssistant
2
+
3
+ A collection of testing tools, hacks, and utilities for writing and fixing tests faster
4
+
5
+ ## Stability
6
+
7
+ TestAssistant is in its infancy and should be considered unstable. The API and behaviour is likely to change.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ group :test do
15
+ gem 'test_assistant'
16
+ end
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install test_assistant
25
+
26
+ ## Usage
27
+
28
+ Test assistant requires access to the RSpec configuration object, so add the following to either `rails_helper.rb` or `spec_helper.rb`:
29
+
30
+ ```ruby
31
+ RSpec.configure do |config|
32
+ # other rspec configuration
33
+
34
+ TestAssistant.configure(config) do |ta_config|
35
+ # test assistant configuration here
36
+ end
37
+ end
38
+ ```
39
+
40
+ ### Rendering a response context when a test fails
41
+
42
+ Test assistant can automatically render the server response in your browser when a test fails and you have applied a nominated tag.
43
+
44
+ ```ruby
45
+ TestAssistant.configure(config) do |ta_config|
46
+ ta_config.render_failed_responses tag: :focus, type: :request
47
+ end
48
+ ```
49
+
50
+ ```ruby
51
+ RSpec.describe 'making some valid request', type: :request do
52
+ context 'some important context' do
53
+ it 'should return a correct result', focus: true do
54
+ # failing assertions
55
+ end
56
+ end
57
+ end
58
+ ```
59
+
60
+ ### Invoking a debugger when a test fails
61
+
62
+ It's possible to invoke a debugger (`pry` is default, but fallback is to `byebug` and then `debugger`) if a test fails. This gives you access to some of the scope that the failing test ran in, allowing you to inspect objects and test variations of the failing assertion.
63
+
64
+ The `debug_failed_responses` accepts a the following options:
65
+
66
+ * `tag: :<tag_name>` (default is `:debugger`)
67
+ * `type: :<spec_type>` (default: nil - matches all test types) options.
68
+
69
+ ```ruby
70
+ TestAssistant.configure(config) do |ta_config|
71
+ ta_config.include_json_helpers type: :request
72
+
73
+ ta_config.debug_failed_responses tag: :debugger
74
+ end
75
+ ```
76
+
77
+ ```ruby
78
+ RSpec.describe 'making some valid request', type: :request do
79
+ context 'some important context' do
80
+ it 'should return a correct result', debugger: true do
81
+ # failing assertions
82
+ end
83
+ end
84
+ end
85
+ ```
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it ( https://github.com/[my-github-username]/test_assistant/fork )
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,51 @@
1
+ module TestAssistant
2
+ autoload :JSONHelpers, 'test_assistant/json_helpers'
3
+ autoload :FailureReporter, 'test_assistant/failure_reporter'
4
+
5
+ class Configuration
6
+ def initialize(rspec_config)
7
+ @rspec_config = rspec_config
8
+ end
9
+
10
+ def include_json_helpers(options = {})
11
+ @rspec_config.include JSONHelpers, options
12
+ end
13
+
14
+ def render_failed_responses(options = {})
15
+ tag_filter = options[:tag]
16
+ no_tag_filter = !tag_filter
17
+ type_filter = options[:type]
18
+ no_type_filter = !type_filter
19
+
20
+ @rspec_config.after(:each) do |example|
21
+ if example.exception
22
+ if (example.metadata[tag_filter] || no_tag_filter) && (example.metadata[:type] == type_filter || no_type_filter)
23
+ reporter = FailureReporter.report(request, response)
24
+ reporter.write
25
+ reporter.open
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def debug_failed_responses(options = {})
32
+ tag_filter = options.fetch(:tag, :debugger)
33
+ type_filter = options[:type]
34
+ no_type_filter = !type_filter
35
+
36
+ @rspec_config.after(:each) do |example|
37
+ if example.exception
38
+ if (example.metadata[tag_filter]) && (example.metadata[:type] == type_filter || no_type_filter)
39
+ if defined? binding
40
+ binding.pry
41
+ elsif defined? byebug
42
+ byebug
43
+ else
44
+ debugger
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,95 @@
1
+ module TestAssistant
2
+ class FailureReporter
3
+ class SummaryReporter
4
+ attr_accessor :next, :file_extension
5
+
6
+ def initialize(request, response, extension = file_extension)
7
+ @request, @response, @extension = request, response, extension
8
+ end
9
+
10
+ def write
11
+ File.open(file_path, 'w') do |file|
12
+ file.write(summary)
13
+ end
14
+ end
15
+
16
+ def open
17
+ system "open #{file_path}"
18
+ end
19
+
20
+ def summary
21
+ @response.body
22
+ end
23
+
24
+ protected
25
+
26
+ def file_path
27
+ @file_path ||= "#{Rails.root}/tmp/#{DateTime.now.to_i}.#{@extension}"
28
+ end
29
+ end
30
+
31
+ class JsonReporter < SummaryReporter
32
+ protected
33
+
34
+ def summary
35
+ parsed_json =
36
+ begin
37
+ JSON.parse(@response.body)
38
+ rescue JSON::ParserError
39
+ @response.body
40
+ end
41
+
42
+ {
43
+ request: {
44
+ path: @request.path,
45
+ cookies: @request.cookies,
46
+ content_type: @request.content_type,
47
+ format: @request.format,
48
+ referrer: @request.referrer
49
+ },
50
+ response: {
51
+ status: "#{@response.status} #{@response.status_message}",
52
+ cookies: @response.cookies,
53
+ redirect_url: @response.redirect_url
54
+ },
55
+ headers: @response.headers.to_h,
56
+ body: parsed_json
57
+ }.to_json
58
+ end
59
+
60
+ def file_extension
61
+ 'json'.freeze
62
+ end
63
+ end
64
+
65
+ class TextReporter < SummaryReporter
66
+ protected
67
+
68
+ def summary
69
+ "<pre>#{@response.body}</pre>"
70
+ end
71
+
72
+ def file_extension
73
+ 'html'.freeze
74
+ end
75
+ end
76
+
77
+ def self.report(request, response)
78
+ extension = file_extension(response.content_type)
79
+
80
+ case extension
81
+ when 'txt'
82
+ TextReporter.new(request, response)
83
+ when 'json'
84
+ JsonReporter.new(request, response)
85
+ else
86
+ SummaryReporter.new(request, response, extension)
87
+ end
88
+ end
89
+
90
+ def self.file_extension(content_type)
91
+ MIME::Types[content_type].first.extensions.first
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,9 @@
1
+ module TestAssistant::JSONHelpers
2
+ def json_response
3
+ begin
4
+ JSON.parse(response.body)
5
+ rescue JSON::ParserError
6
+ '< INVALID JSON RESPONSE >'.freeze
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module TestAssistant
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ require "test_assistant/version"
2
+ require 'test_assistant/configuration'
3
+
4
+ module TestAssistant
5
+ class << self
6
+ def configure(rspec_config)
7
+ configuration = Configuration.new(rspec_config)
8
+
9
+ if block_given?
10
+ yield(configuration)
11
+ end
12
+ end
13
+
14
+ alias :config :configure
15
+ end
16
+
17
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'test_assistant/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "test_assistant"
8
+ spec.version = TestAssistant::VERSION
9
+ spec.authors = ["Aleck Greenham"]
10
+ spec.email = ["greenhama13@gmail.com"]
11
+ spec.summary = "A toolbox for increased testing efficiency with RSpec"
12
+ spec.description = "A collection of testing tools, hacks, and utilities for writing and fixing tests faster"
13
+ spec.homepage = "https://github.com/greena13/test_assistant"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency "rspec-rails", "~> 3.0"
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake", "~> 0"
24
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test_assistant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aleck Greenham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A collection of testing tools, hacks, and utilities for writing and fixing
56
+ tests faster
57
+ email:
58
+ - greenhama13@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/test_assistant.rb
69
+ - lib/test_assistant/configuration.rb
70
+ - lib/test_assistant/failure_reporter.rb
71
+ - lib/test_assistant/json_helpers.rb
72
+ - lib/test_assistant/version.rb
73
+ - test_assistant.gemspec
74
+ homepage: https://github.com/greena13/test_assistant
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A toolbox for increased testing efficiency with RSpec
98
+ test_files: []