rspec-active_record 0.3.2 → 0.5.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 +4 -4
- data/.rubocop.yml +8 -3
- data/CHANGELOG.md +8 -0
- data/Gemfile +7 -0
- data/README.md +10 -0
- data/lib/rspec/active_record/change_record.rb +10 -5
- data/lib/rspec/active_record/create_record.rb +16 -10
- data/lib/rspec/active_record/stub_models.rb +1 -0
- data/lib/rspec/active_record/version.rb +1 -1
- data/lib/rspec/active_record.rb +1 -1
- data/rspec-active_record.gemspec +1 -6
- metadata +3 -72
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a7a6ee8d2f8e4e3230227c54f7b6ac9484820e2d7316b7a286529f7b96fcf168
|
|
4
|
+
data.tar.gz: 7544437ce608ee8190a98631c3a9ac81839e9a137f598826383e90950e8d7ea8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e6e48ae2fef14172a456daa012eea9eeb94f53ddabb54db89e5b469f6d51c68b55386374b58836358d311d1fb77353bbffc4768927123de75968882bf2ce2b4f
|
|
7
|
+
data.tar.gz: acdf577a588b6da64e5233ab0c875cbbc38d8896218f24ef2f298928c5697b4b5fb6271671910a32f1358c82e263600a30270b36a729afcf37e11c833bfcf613
|
data/.rubocop.yml
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
AllCops:
|
|
2
2
|
TargetRubyVersion: 2.6
|
|
3
|
+
NewCops: enable
|
|
4
|
+
|
|
5
|
+
Layout/LineLength:
|
|
6
|
+
Max: 120
|
|
3
7
|
|
|
4
8
|
Metrics/BlockLength:
|
|
5
9
|
Exclude:
|
|
@@ -9,6 +13,10 @@ Metrics/BlockLength:
|
|
|
9
13
|
Metrics/MethodLength:
|
|
10
14
|
Enabled: false
|
|
11
15
|
|
|
16
|
+
Naming/PredicatePrefix:
|
|
17
|
+
AllowedMethods:
|
|
18
|
+
- does_not_match?
|
|
19
|
+
|
|
12
20
|
Style/StringLiterals:
|
|
13
21
|
Enabled: true
|
|
14
22
|
EnforcedStyle: double_quotes
|
|
@@ -16,6 +24,3 @@ Style/StringLiterals:
|
|
|
16
24
|
Style/StringLiteralsInInterpolation:
|
|
17
25
|
Enabled: true
|
|
18
26
|
EnforcedStyle: double_quotes
|
|
19
|
-
|
|
20
|
-
Layout/LineLength:
|
|
21
|
-
Max: 120
|
data/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
|
+
## [0.5.0] - 2026-10-26
|
|
3
|
+
|
|
4
|
+
- Implement `change_record.attributes`, which allows shortening `change_record.from`.
|
|
5
|
+
|
|
6
|
+
## [0.4.0] - 2026-10-02
|
|
7
|
+
|
|
8
|
+
- Allow `create_record.matching` to take multiple attribute hashes
|
|
9
|
+
- Fix query cache causing issues between differently structured tables
|
|
2
10
|
|
|
3
11
|
## [0.3.2] - 2025-03-12
|
|
4
12
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -43,6 +43,8 @@ You can also make sure that attributes match, if it fails you'll get RSpec diff
|
|
|
43
43
|
|
|
44
44
|
```ruby
|
|
45
45
|
expect { User.create!(name: "RSpec User") }.to create_record(User).matching(name: "RSpec User")
|
|
46
|
+
expect { User.create!([{name: "RSpec User"}, {name: "Test User"}]) }
|
|
47
|
+
.to create_record(User).matching({name: "RSpec User"}, {name: "Test User"})
|
|
46
48
|
```
|
|
47
49
|
|
|
48
50
|
You can also achieve similar results using a scope, but not that in this case you won't see a diff:
|
|
@@ -64,6 +66,14 @@ expect { user.update!(name: "RSpec User") }.to change_record(user).from(name: "I
|
|
|
64
66
|
expect { user.name = "RSpec User" }.to not_change_record(user).from(name: "Initial Name")
|
|
65
67
|
```
|
|
66
68
|
|
|
69
|
+
Use `attributes` to take the initial values from the record, equivalent to
|
|
70
|
+
`from(name: user.name, id: user.id)`:
|
|
71
|
+
```ruby
|
|
72
|
+
expect { user.update!(name: "RSpec User", email: "user@example.com") }
|
|
73
|
+
.to change_record(user).attributes(:name, :email)
|
|
74
|
+
expect { user.name = "RSpec User" }.to not_change_record(user).attributes(:name, :id)
|
|
75
|
+
```
|
|
76
|
+
|
|
67
77
|
### destroy_record
|
|
68
78
|
|
|
69
79
|
Check that code destroys a record:
|
|
@@ -18,15 +18,20 @@ module RSpec
|
|
|
18
18
|
self
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
+
# Use the record's current values as the initial attributes
|
|
22
|
+
def attributes(*names)
|
|
23
|
+
from(names.index_with { |name| @record.public_send(name) })
|
|
24
|
+
end
|
|
25
|
+
|
|
21
26
|
def matches?(block)
|
|
22
27
|
@from_diff = diff_unless_match(@record, @from)
|
|
23
|
-
@to_pre_match = !
|
|
28
|
+
@to_pre_match = !not_match_attributes?(@record, @to)
|
|
24
29
|
|
|
25
30
|
block.call
|
|
26
31
|
@updated_record = @record.class.base_class.find(@record.id)
|
|
27
32
|
|
|
28
33
|
@to_diff = diff_unless_match(@updated_record, @to)
|
|
29
|
-
@from_post_match = !
|
|
34
|
+
@from_post_match = !not_match_attributes?(@updated_record, @from)
|
|
30
35
|
|
|
31
36
|
(@from || @to) && !@from_diff && !@to_pre_match && !@to_diff && !@from_post_match
|
|
32
37
|
end
|
|
@@ -60,7 +65,7 @@ module RSpec
|
|
|
60
65
|
elsif @to_diff
|
|
61
66
|
"expected to change #{format_record} to #{format_hash(@to)} but did not#{@to_diff}"
|
|
62
67
|
else
|
|
63
|
-
"please specify from or to for change_record"
|
|
68
|
+
"please specify attributes, from or to for change_record"
|
|
64
69
|
end
|
|
65
70
|
end
|
|
66
71
|
|
|
@@ -68,7 +73,7 @@ module RSpec
|
|
|
68
73
|
if @to
|
|
69
74
|
"negative matcher for change_record with to is not supported"
|
|
70
75
|
elsif @from.blank?
|
|
71
|
-
"negative matcher for change_record without from is not supported"
|
|
76
|
+
"negative matcher for change_record without attributes or from is not supported"
|
|
72
77
|
elsif @from_diff
|
|
73
78
|
"expected #{format_record} to match #{format_hash(@from)} initially but it did not#{@from_diff}"
|
|
74
79
|
elsif !@from_post_match
|
|
@@ -92,7 +97,7 @@ module RSpec
|
|
|
92
97
|
RSpec::Matchers::BuiltIn::HaveAttributes.new(attributes).matches?(record)
|
|
93
98
|
end
|
|
94
99
|
|
|
95
|
-
def
|
|
100
|
+
def not_match_attributes?(record, attributes)
|
|
96
101
|
return true unless attributes
|
|
97
102
|
|
|
98
103
|
RSpec::Matchers::BuiltIn::HaveAttributes.new(attributes).does_not_match?(record)
|
|
@@ -8,11 +8,12 @@ module RSpec
|
|
|
8
8
|
|
|
9
9
|
def initialize(scope)
|
|
10
10
|
@scope = scope
|
|
11
|
+
@attributes = []
|
|
11
12
|
super()
|
|
12
13
|
end
|
|
13
14
|
|
|
14
15
|
# Make sure that created record matches attributes
|
|
15
|
-
def matching(attributes)
|
|
16
|
+
def matching(*attributes)
|
|
16
17
|
@attributes = attributes
|
|
17
18
|
self
|
|
18
19
|
end
|
|
@@ -44,22 +45,26 @@ module RSpec
|
|
|
44
45
|
|
|
45
46
|
@new_records = @scope.where.not(@scope.primary_key => existing_ids).to_a
|
|
46
47
|
|
|
47
|
-
match_times? && match_attributes? && @new_records.present?
|
|
48
|
+
match_times? && @attributes.all? { |attrs| match_attributes?(attrs) } && @new_records.present?
|
|
48
49
|
end
|
|
49
50
|
|
|
50
51
|
def description
|
|
52
|
+
match_messages = @attributes.map do |attrs|
|
|
53
|
+
" matching #{format_hash(attrs)}"
|
|
54
|
+
end
|
|
55
|
+
|
|
51
56
|
message = "create #{scope_name}"
|
|
52
57
|
message += " #{@times} time#{"s" if @times != 1}" if @times
|
|
53
|
-
message += "
|
|
58
|
+
message += match_messages.join(",")
|
|
54
59
|
improve_hash_formatting message
|
|
55
60
|
end
|
|
56
61
|
|
|
57
62
|
def failure_message
|
|
58
63
|
failure = "expected to #{description} but"
|
|
59
|
-
if
|
|
60
|
-
"#{failure} created #{@new_records.size}"
|
|
61
|
-
else
|
|
64
|
+
if match_times?
|
|
62
65
|
add_diff "#{failure} did not"
|
|
66
|
+
else
|
|
67
|
+
"#{failure} created #{@new_records.size}"
|
|
63
68
|
end
|
|
64
69
|
end
|
|
65
70
|
|
|
@@ -70,7 +75,9 @@ module RSpec
|
|
|
70
75
|
private
|
|
71
76
|
|
|
72
77
|
def add_diff(message)
|
|
73
|
-
|
|
78
|
+
@attributes.each do |attrs|
|
|
79
|
+
message += "\n\n#{DiffForMultipleRecords.new(attrs, @new_records).call}" unless match_attributes?(attrs)
|
|
80
|
+
end
|
|
74
81
|
message
|
|
75
82
|
end
|
|
76
83
|
|
|
@@ -78,9 +85,8 @@ module RSpec
|
|
|
78
85
|
@times.nil? || @times == @new_records.size
|
|
79
86
|
end
|
|
80
87
|
|
|
81
|
-
def match_attributes?
|
|
82
|
-
@
|
|
83
|
-
@new_records.any? { |record| RSpec::Matchers::BuiltIn::HaveAttributes.new(@attributes).matches?(record) }
|
|
88
|
+
def match_attributes?(attrs)
|
|
89
|
+
@new_records.any? { |record| RSpec::Matchers::BuiltIn::HaveAttributes.new(attrs).matches?(record) }
|
|
84
90
|
end
|
|
85
91
|
|
|
86
92
|
def scope_name
|
|
@@ -40,6 +40,7 @@ module RSpec
|
|
|
40
40
|
# Creates temporary table. Only works correctly if DDL transactions are enabled.
|
|
41
41
|
# Takes same arguments as `create_table` in ActiveRecord migration.
|
|
42
42
|
def create_temporary_table(*args, **options, &block)
|
|
43
|
+
::ActiveRecord::Base.connection.clear_cache!
|
|
43
44
|
::ActiveRecord::Base.connection.create_table(*args, **options, &block)
|
|
44
45
|
end
|
|
45
46
|
end
|
data/lib/rspec/active_record.rb
CHANGED
|
@@ -45,7 +45,7 @@ module RSpec
|
|
|
45
45
|
|
|
46
46
|
# Allows matching that code inside a block destroyed specific record.
|
|
47
47
|
# @param record [ActiveRecord::Base] Model that should be destroyed
|
|
48
|
-
# @return [
|
|
48
|
+
# @return [DestroyRecord]
|
|
49
49
|
# @example Block destroyed record
|
|
50
50
|
# expect { user.destroy! }.to destroy_record(user)
|
|
51
51
|
def destroy_record(record)
|
data/rspec-active_record.gemspec
CHANGED
|
@@ -14,6 +14,7 @@ Gem::Specification.new do |spec|
|
|
|
14
14
|
spec.license = "MIT"
|
|
15
15
|
spec.required_ruby_version = ">= 2.6.0"
|
|
16
16
|
|
|
17
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
17
18
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
19
|
spec.metadata["source_code_uri"] = spec.homepage
|
|
19
20
|
spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
|
|
@@ -33,12 +34,6 @@ Gem::Specification.new do |spec|
|
|
|
33
34
|
spec.add_dependency "rspec-expectations"
|
|
34
35
|
spec.add_dependency "rspec-mocks"
|
|
35
36
|
|
|
36
|
-
spec.add_development_dependency "database_cleaner-active_record"
|
|
37
|
-
spec.add_development_dependency "rake"
|
|
38
|
-
spec.add_development_dependency "rspec"
|
|
39
|
-
spec.add_development_dependency "rubocop"
|
|
40
|
-
spec.add_development_dependency "sqlite3"
|
|
41
|
-
|
|
42
37
|
# For more information and examples about making a new gem, check out our
|
|
43
38
|
# guide at: https://bundler.io/guides/creating_gem.html
|
|
44
39
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rspec-active_record
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrius Chamentauskas
|
|
@@ -51,76 +51,6 @@ dependencies:
|
|
|
51
51
|
- - ">="
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
53
|
version: '0'
|
|
54
|
-
- !ruby/object:Gem::Dependency
|
|
55
|
-
name: database_cleaner-active_record
|
|
56
|
-
requirement: !ruby/object:Gem::Requirement
|
|
57
|
-
requirements:
|
|
58
|
-
- - ">="
|
|
59
|
-
- !ruby/object:Gem::Version
|
|
60
|
-
version: '0'
|
|
61
|
-
type: :development
|
|
62
|
-
prerelease: false
|
|
63
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
-
requirements:
|
|
65
|
-
- - ">="
|
|
66
|
-
- !ruby/object:Gem::Version
|
|
67
|
-
version: '0'
|
|
68
|
-
- !ruby/object:Gem::Dependency
|
|
69
|
-
name: rake
|
|
70
|
-
requirement: !ruby/object:Gem::Requirement
|
|
71
|
-
requirements:
|
|
72
|
-
- - ">="
|
|
73
|
-
- !ruby/object:Gem::Version
|
|
74
|
-
version: '0'
|
|
75
|
-
type: :development
|
|
76
|
-
prerelease: false
|
|
77
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
-
requirements:
|
|
79
|
-
- - ">="
|
|
80
|
-
- !ruby/object:Gem::Version
|
|
81
|
-
version: '0'
|
|
82
|
-
- !ruby/object:Gem::Dependency
|
|
83
|
-
name: rspec
|
|
84
|
-
requirement: !ruby/object:Gem::Requirement
|
|
85
|
-
requirements:
|
|
86
|
-
- - ">="
|
|
87
|
-
- !ruby/object:Gem::Version
|
|
88
|
-
version: '0'
|
|
89
|
-
type: :development
|
|
90
|
-
prerelease: false
|
|
91
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
-
requirements:
|
|
93
|
-
- - ">="
|
|
94
|
-
- !ruby/object:Gem::Version
|
|
95
|
-
version: '0'
|
|
96
|
-
- !ruby/object:Gem::Dependency
|
|
97
|
-
name: rubocop
|
|
98
|
-
requirement: !ruby/object:Gem::Requirement
|
|
99
|
-
requirements:
|
|
100
|
-
- - ">="
|
|
101
|
-
- !ruby/object:Gem::Version
|
|
102
|
-
version: '0'
|
|
103
|
-
type: :development
|
|
104
|
-
prerelease: false
|
|
105
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
106
|
-
requirements:
|
|
107
|
-
- - ">="
|
|
108
|
-
- !ruby/object:Gem::Version
|
|
109
|
-
version: '0'
|
|
110
|
-
- !ruby/object:Gem::Dependency
|
|
111
|
-
name: sqlite3
|
|
112
|
-
requirement: !ruby/object:Gem::Requirement
|
|
113
|
-
requirements:
|
|
114
|
-
- - ">="
|
|
115
|
-
- !ruby/object:Gem::Version
|
|
116
|
-
version: '0'
|
|
117
|
-
type: :development
|
|
118
|
-
prerelease: false
|
|
119
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
120
|
-
requirements:
|
|
121
|
-
- - ">="
|
|
122
|
-
- !ruby/object:Gem::Version
|
|
123
|
-
version: '0'
|
|
124
54
|
description: RSpec matchers to check when ActiveRecord objects are created/updated/destroyed
|
|
125
55
|
email:
|
|
126
56
|
- andrius.chamentauskas@gmail.com
|
|
@@ -150,6 +80,7 @@ homepage: https://github.com/andriusch/rspec-active_record
|
|
|
150
80
|
licenses:
|
|
151
81
|
- MIT
|
|
152
82
|
metadata:
|
|
83
|
+
rubygems_mfa_required: 'true'
|
|
153
84
|
homepage_uri: https://github.com/andriusch/rspec-active_record
|
|
154
85
|
source_code_uri: https://github.com/andriusch/rspec-active_record
|
|
155
86
|
changelog_uri: https://github.com/andriusch/rspec-active_record/CHANGELOG.md
|
|
@@ -167,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
167
98
|
- !ruby/object:Gem::Version
|
|
168
99
|
version: '0'
|
|
169
100
|
requirements: []
|
|
170
|
-
rubygems_version: 4.0.
|
|
101
|
+
rubygems_version: 4.0.19
|
|
171
102
|
specification_version: 4
|
|
172
103
|
summary: RSpec matchers for ActiveRecord
|
|
173
104
|
test_files: []
|