diff --git a/CHANGELOG.md b/CHANGELOG.md index 8765a853..79782922 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index e87877ca..3a41bf87 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/paper_trail/frameworks/rspec.rb b/lib/paper_trail/frameworks/rspec.rb index 9a84a3f2..a03f544f 100644 --- a/lib/paper_trail/frameworks/rspec.rb +++ b/lib/paper_trail/frameworks/rspec.rb @@ -27,3 +27,8 @@ RSpec::Matchers.define :have_a_version_with do |attributes| # 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 diff --git a/spec/models/widget_spec.rb b/spec/models/widget_spec.rb index 5fd563fb..21388b50 100644 --- a/spec/models/widget_spec.rb +++ b/spec/models/widget_spec.rb @@ -21,6 +21,20 @@ describe Widget, type: :model do 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