assert_valid 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 +7 -0
- data/.gitignore +8 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +19 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +4 -0
- data/assert_valid.gemspec +35 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/assert_valid.rb +8 -0
- data/lib/assert_valid/assertions.rb +36 -0
- data/lib/assert_valid/version.rb +5 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8115bc34bd047a98c7cdf40de957cd50d579460de87c303d0c476b4a189ae4c3
|
4
|
+
data.tar.gz: 3c7797ec57d97cc5fd2b0285e49580994fa7b7e54a388279f050191f91402087
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d1c76171acc87d6d4543bfac10f64dd9795b9660c0c33ab3f751f83b006a75be00de3cdb4da46120278f9a8d2f56f51ea26b84d500d3b7038337beddfa31a71
|
7
|
+
data.tar.gz: a46f373a2de5e42507f5288ef3a4e39e1056f7d6a628de0e6db94d6a45e96a0a63a562e8d808186acb43fc6d187d6994c95ad461e154fc56e6fb7ac252e44dae
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Brendan Tang
|
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,61 @@
|
|
1
|
+
# AssertValid
|
2
|
+
|
3
|
+
A tiny gem that provides that provides two utility methods for testing ActiveRecord models: `assert_valid` and `assert_invalid`.
|
4
|
+
|
5
|
+
This gem could easily just be a GitHub gist—the methods are written in less than 50 lines.
|
6
|
+
|
7
|
+
But I've copy and pasted them enough times that I just made a gem.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this gem to your Rails app's development group:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
group :test do
|
15
|
+
# ...
|
16
|
+
gem 'assert_valid'
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
And then `bundle install`.
|
21
|
+
|
22
|
+
Include the module in `test/test_helper.rb`:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class ActiveSupport::TestCase
|
26
|
+
include AssertValid::Assertions
|
27
|
+
# ...
|
28
|
+
```
|
29
|
+
|
30
|
+
Or instead of using the gem, you could just copy the methods as defined in `lib/assert_valid/assertions.rb` and paste them into your `test/test_helper.rb`.
|
31
|
+
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
In your unit tests, you can use `assert_valid` or `assert_invalid`, passing in the record you want to check.
|
36
|
+
|
37
|
+
`assert_invalid` also optionally takes an attribute name to check for an error on that attribute specifically.
|
38
|
+
|
39
|
+
If you specify an attribute, you can also specify an error kind. Example:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# app/models/user.rb
|
43
|
+
class User < ApplicationRecord
|
44
|
+
validates :name, uniqueness: true
|
45
|
+
#...
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# test/models/user_test.rb
|
51
|
+
|
52
|
+
test "validates unique name" do
|
53
|
+
User.create(name: "bob")
|
54
|
+
duplicated_user = User.new(name: "bob")
|
55
|
+
assert_invalid(duplicated_user, :name, :taken)
|
56
|
+
```
|
57
|
+
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
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,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/assert_valid/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "assert_valid"
|
7
|
+
spec.version = AssertValid::VERSION
|
8
|
+
spec.authors = ["Brendan Tang"]
|
9
|
+
spec.email = ["b@brendantang.net"]
|
10
|
+
|
11
|
+
spec.summary = "Provides assert_valid and assert_invalid utility methods for testing ActiveRecord models."
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/brendantang/assert_valid"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
19
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
# Uncomment to register a new dependency of your gem
|
31
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
32
|
+
|
33
|
+
# For more information and examples about making a new gem, checkout our
|
34
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
35
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "assert_valid"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/assert_valid.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module AssertValid
|
2
|
+
module Assertions
|
3
|
+
def assert_invalid(invalid_record, attribute_name = nil, error_kind = nil)
|
4
|
+
assert_not(
|
5
|
+
invalid_record.valid?,
|
6
|
+
"No validation errors raised. Record: #{invalid_record.inspect}"
|
7
|
+
)
|
8
|
+
if attribute_name
|
9
|
+
assert_includes(
|
10
|
+
invalid_record.errors.attribute_names,
|
11
|
+
attribute_name,
|
12
|
+
"Expected record to have a(n) #{attribute_name} error.
|
13
|
+
Errors: #{invalid_record.errors.details}
|
14
|
+
Record: #{invalid_record.inspect}"
|
15
|
+
)
|
16
|
+
if error_kind
|
17
|
+
assert(
|
18
|
+
invalid_record.errors.details[attribute_name].any? do |err_detail|
|
19
|
+
err_detail[:error] == error_kind
|
20
|
+
end,
|
21
|
+
"Expected #{attribute_name} errors to include a #{error_kind} error.
|
22
|
+
#{attribute_name} errors were: #{invalid_record.errors.details}"
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def assert_valid(record)
|
29
|
+
assert(
|
30
|
+
record.save,
|
31
|
+
"Record could not be saved. Record: #{record.inspect}
|
32
|
+
Errors: #{record.errors.details}"
|
33
|
+
)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: assert_valid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brendan Tang
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Provides assert_valid and assert_invalid utility methods for testing
|
14
|
+
ActiveRecord models.
|
15
|
+
email:
|
16
|
+
- b@brendantang.net
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- assert_valid.gemspec
|
28
|
+
- bin/console
|
29
|
+
- bin/setup
|
30
|
+
- lib/assert_valid.rb
|
31
|
+
- lib/assert_valid/assertions.rb
|
32
|
+
- lib/assert_valid/version.rb
|
33
|
+
homepage: https://github.com/brendantang/assert_valid
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
37
|
+
homepage_uri: https://github.com/brendantang/assert_valid
|
38
|
+
source_code_uri: https://github.com/brendantang/assert_valid
|
39
|
+
changelog_uri: https://github.com/brendantang/assert_valid
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.4.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.2.16
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Provides assert_valid and assert_invalid utility methods for testing ActiveRecord
|
59
|
+
models.
|
60
|
+
test_files: []
|