Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add have_a_version_with_changes matcher #881

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Added

- None
- [#881](https://github.com/airblade/paper_trail/pull/881) - Add RSpec matcher
`have_a_version_with_changes` for easier testing.

### Fixed

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,8 @@ end
```

It is also possible to do assertions on the versions using `have_a_version_with`
matcher
and `have_a_version_with_changes` matchers. For more examples, see the
[Widget spec](https://github.com/airblade/paper_trail/blob/master/spec/models/widget_spec.rb)

```ruby
describe '`have_a_version_with` matcher' do
Expand Down
5 changes: 5 additions & 0 deletions lib/paper_trail/frameworks/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@
# check if the model has a version with the specified attributes
match { |actual| actual.versions.where_object(attributes).any? }
end

RSpec::Matchers.define :have_a_version_with_changes do |attributes|
# check if the model has a version changes with the specified attributes
match { |actual| actual.versions.where_object_changes(attributes).any? }
end
14 changes: 14 additions & 0 deletions spec/models/widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
end
end

describe "`have_a_version_with_changes` matcher", versioning: true do
before do
widget.update_attributes!(name: "Leonard", an_integer: 2)
widget.update_attributes!(name: "Tom")
widget.update_attributes!(name: "Bob")
end

it "is possible to do assertions on versions" do
expect(widget).to have_a_version_with_changes name: "Leonard", an_integer: 2
expect(widget).to have_a_version_with_changes an_integer: 2
expect(widget).to have_a_version_with_changes name: "Tom"
end
end

describe "versioning option" do
context "enabled", versioning: true do
it "should enable versioning" do
Expand Down