rspec-request_snapshot 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 91f5656a0f745a0e13bee0f04738a1fc60a9217b614458433a12e0eb0734c300
4
+ data.tar.gz: 513056854ad49b8f92a26dbb7ac592542ef406b46690972a197ec75fb9516e8f
5
+ SHA512:
6
+ metadata.gz: fe5490d3028654103286d208ac0b8f41ef5c801431ac2fea3db6215328ab17a59ea9905bf0a0a776cb5834d0217436520963583ff6b79cb9a696e94f23d36cd9
7
+ data.tar.gz: 6917720effe7ade229edad8c50196cdef162f4150da9c95a63afe131a0031986ba810e19f855484d73f702a5e0efbc373b1ab25852a8c270e63bcec4cc50eec3
@@ -0,0 +1,43 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.5-node-browsers
6
+
7
+ working_directory: ~/repo
8
+
9
+ steps:
10
+ - checkout
11
+
12
+ - restore_cache:
13
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
14
+
15
+ - run:
16
+ name: install dependencies
17
+ command: |
18
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
19
+
20
+ - save_cache:
21
+ paths:
22
+ - ./vendor/bundle
23
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
24
+
25
+ - run:
26
+ name: Download CodeClimate cc-test-reporter
27
+ command: curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
28
+
29
+ - run:
30
+ name: Make cc-test-reporter executable
31
+ command: chmod +x ./cc-test-reporter
32
+
33
+ - run:
34
+ name: Report before build to CodeClimate
35
+ command: ./cc-test-reporter before-build
36
+
37
+ - run:
38
+ name: run tests
39
+ command: bundle exec rspec
40
+
41
+ - run:
42
+ name: Report after build to CodeClimate
43
+ command: ./cc-test-reporter after-build --coverage-input-type simplecov --exit-code $?
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,59 @@
1
+ # require: rubocop-rspec
2
+
3
+ Style/Lambda:
4
+ EnforcedStyle: literal
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
8
+
9
+ Metrics/LineLength:
10
+ Max: 120
11
+ AllowURI: true
12
+
13
+ Metrics/BlockLength:
14
+ Exclude:
15
+ - "spec/**/*"
16
+
17
+ Documentation:
18
+ Enabled: false
19
+
20
+ RSpec/NestedGroups:
21
+ Enabled: false
22
+
23
+ Style/ClassAndModuleChildren:
24
+ EnforcedStyle: compact
25
+
26
+ Layout/FirstHashElementLineBreak:
27
+ Enabled: true
28
+
29
+ Style/BlockDelimiters:
30
+ Exclude:
31
+ - "spec/**/*"
32
+
33
+ Style/GlobalVars:
34
+ AllowedVariables:
35
+ - "$rails_rake_task"
36
+
37
+ Style/PercentLiteralDelimiters:
38
+ PreferredDelimiters:
39
+ '%w': '()'
40
+ '%W': '()'
41
+ '%i': '()'
42
+ '%I': '()'
43
+
44
+ Style/FormatStringToken:
45
+ EnforcedStyle: template
46
+
47
+ Naming/PredicateName:
48
+ NamePrefixBlacklist:
49
+ - is_
50
+
51
+ # Custom Checks
52
+
53
+ Lint/AmbiguousBlockAssociation:
54
+ Exclude:
55
+ - "spec/**/*"
56
+
57
+ AllCops:
58
+ Exclude:
59
+ - "*.gemspec"
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rspec-request_snapshot.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rspec-request_snapshot (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ docile (1.3.1)
11
+ json (2.1.0)
12
+ rake (10.5.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.0)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.2)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.0)
26
+ simplecov (0.16.1)
27
+ docile (~> 1.1)
28
+ json (>= 1.8, < 3)
29
+ simplecov-html (~> 0.10.0)
30
+ simplecov-html (0.10.2)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ bundler (~> 1.16)
37
+ rake (~> 10.0)
38
+ rspec (~> 3.0)
39
+ rspec-request_snapshot!
40
+ simplecov (~> 0.16.1)
41
+
42
+ BUNDLED WITH
43
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 CareMessage
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Rspec::RequestSnapshot
2
+
3
+ Gold master testing for RSpec API request specs. Make sure API behavior is not changing by taking and storing a snapshot from an API response on a first run and check if they match on next spec runs.
4
+
5
+ By default, snapshots are stored under `spec/fixtures/snapshots` and should be code reviewed as well. The syntax is inspired by Jest.
6
+
7
+ References:
8
+ - [Gold Master Testing article by CodeClimate](https://codeclimate.com/blog/gold-master-testing/)
9
+ - [Jest Snapshot Testing](https://jestjs.io/docs/en/snapshot-testing)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'rspec-request_snapshot'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install rspec-request_snapshot
26
+
27
+ ## Usage
28
+
29
+ ### Configuration
30
+
31
+ ```ruby
32
+ RSpec.configure do |config|
33
+ # The place where snapshots will be stored
34
+ # Default value is spec/fixtures/snapshots
35
+ config.request_snapshots_dir = "spec/fixtures/snapshots"
36
+
37
+ # The json attributes that you want to ignore when comparing snapshots
38
+ # Default value is [id, created_at, updated_at]
39
+ config.request_snapshots_dynamic_attributes = %w(id created_at updated_at)
40
+ end
41
+ ```
42
+
43
+ ### Snapshot files
44
+
45
+ On the first run, the `match_snapshot` matcher will always return success and it will store a snapshot file. On the next runs, it will compare the response with the file content.
46
+
47
+ If you need to replace snapshots, run the specs with:
48
+
49
+ REPLACE_SNAPSHOTS=true bundle exec rspec
50
+
51
+ ### Matcher
52
+
53
+ ```ruby
54
+ # Stores snapshot under spec/fixtures/snapshots/api/resources_index.json
55
+ expect(response.body).to match_snapshot("api/resources_index")
56
+
57
+ # Using plain text instead of parsing JSON
58
+ expect(response.body).to match_snapshot("api/resources_index", format: :text)
59
+
60
+ # Defining specific test dynamic attributes
61
+ expect(response.body).to match_snapshot("api/resources_index", dyanmic_attributes: %w(confirmed_at relation_id))
62
+ ```
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/CareMessagePlatform/rspec-request_snapshot.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rspec/request_snapshot"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ module Rspec::RequestSnapshot
2
+ RSpec.configure do |config|
3
+ config.add_setting :request_snapshots_dir, default: "spec/fixtures/snapshots"
4
+ config.add_setting :request_snapshots_dynamic_attributes, default: %w(id created_at updated_at)
5
+ end
6
+ end
@@ -0,0 +1,67 @@
1
+ require "json"
2
+
3
+ module Rspec::RequestSnapshot
4
+ RSpec::Matchers.define :match_snapshot do |snapshot_name, options|
5
+ attr_reader :actual, :expected, :options
6
+
7
+ diffable
8
+
9
+ match do |actual|
10
+ @options = options || {}
11
+
12
+ snapshot_file_path = File.join(RSpec.configuration.request_snapshots_dir, "#{snapshot_name}.json")
13
+
14
+ FileUtils.mkdir_p(File.dirname(snapshot_file_path)) unless Dir.exist?(File.dirname(snapshot_file_path))
15
+
16
+ if File.exist?(snapshot_file_path) && !(ENV["REPLACE_SNAPSHOTS"] == "true")
17
+ @actual = comparable(replace_dynamic_attributes(actual))
18
+ @expected = comparable(replace_dynamic_attributes(File.read(snapshot_file_path)))
19
+ @actual == @expected
20
+ else
21
+ File.write(snapshot_file_path, writable(comparable(@actual)))
22
+ true
23
+ end
24
+ end
25
+
26
+ failure_message do
27
+ [
28
+ "expected: #{expected}",
29
+ " got: #{actual}"
30
+ ].join("\n")
31
+ end
32
+
33
+ def comparable(str)
34
+ case format
35
+ when :text
36
+ str
37
+ when :json
38
+ JSON.parse(str)
39
+ end
40
+ end
41
+
42
+ def writable(str)
43
+ case format
44
+ when :text
45
+ str
46
+ when :json
47
+ JSON.pretty_generate(str)
48
+ end
49
+ end
50
+
51
+ def format
52
+ @options[:format]&.to_sym || :json
53
+ end
54
+
55
+ def replace_dynamic_attributes(json)
56
+ dynamic_attributes.each do |attribute|
57
+ json.gsub!(/\"#{attribute}\":\s*(\".*?\"|\d+|\w+)/, "\"#{attribute}\":\"REPLACED\"")
58
+ end
59
+ json
60
+ end
61
+
62
+ def dynamic_attributes
63
+ @dynamic_attributes ||= RSpec.configuration.request_snapshots_dynamic_attributes |
64
+ Array(options[:dynamic_attributes])
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module RequestSnapshot
3
+ VERSION = "0.1.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require "rspec/request_snapshot/version"
2
+ require "rspec/request_snapshot/config"
3
+ require "rspec/request_snapshot/matcher"
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "rspec/request_snapshot/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rspec-request_snapshot"
7
+ spec.version = Rspec::RequestSnapshot::VERSION
8
+ spec.authors = ["Bruno Campos"]
9
+ spec.email = ["bcampos@caremessage.org"]
10
+
11
+ spec.summary = "Gold master testing for RSpec API request specs"
12
+ spec.description = "Make sure API behavior is not changing by taking and storing a snapshot from an API response on a first run and check if they match on next spec runs."
13
+ spec.homepage = "https://github.com/CareMessagePlatform/rspec-request_snapshot"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "simplecov", "~> 0.16.1"
29
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-request_snapshot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Campos
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.16.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.16.1
69
+ description: Make sure API behavior is not changing by taking and storing a snapshot
70
+ from an API response on a first run and check if they match on next spec runs.
71
+ email:
72
+ - bcampos@caremessage.org
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".circleci/config.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".rubocop.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/rspec/request_snapshot.rb
89
+ - lib/rspec/request_snapshot/config.rb
90
+ - lib/rspec/request_snapshot/matcher.rb
91
+ - lib/rspec/request_snapshot/version.rb
92
+ - rspec-request_snapshot.gemspec
93
+ homepage: https://github.com/CareMessagePlatform/rspec-request_snapshot
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Gold master testing for RSpec API request specs
117
+ test_files: []