Skip to content

Commit

Permalink
Merge pull request #394 from emre-basala/version-spec-helper
Browse files Browse the repository at this point in the history
Add RSpec :have_a_version_with matcher
  • Loading branch information
batter committed Jul 9, 2014
2 parents b9b0d17 + a2f1730 commit 80071ba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
23 changes: 21 additions & 2 deletions README.md
Expand Up @@ -969,6 +969,25 @@ describe Widget do
end
```

It is also possible to do assertions on the versions using `have_a_version_with` matcher

```
describe '`have_a_version_with` matcher' do
before do
widget.update_attributes!(:name => 'Leonard', :an_integer => 1 )
widget.update_attributes!(:name => 'Tom')
widget.update_attributes!(:name => 'Bob')
end
it "is possible to do assertions on versions" do
widget.should have_a_version_with :name => 'Leonard', :an_integer => 1
widget.should have_a_version_with :an_integer => 1
widget.should have_a_version_with :name => 'Tom'
end
end
```

### Cucumber

PaperTrail provides a helper for [Cucumber](http://cukes.info) that works similar to the RSpec helper.
Expand Down Expand Up @@ -1024,9 +1043,9 @@ require 'paper_trail/frameworks/rspec'

## Testing PaperTrail

Paper Trail has facilities to test aganist Postgres, Mysql and SQLite. To switch between DB engines you will need to export the DB Variable for the engine you wish to test aganist.
Paper Trail has facilities to test aganist Postgres, Mysql and SQLite. To switch between DB engines you will need to export the DB Variable for the engine you wish to test aganist.

Though be aware we do not have the abilty to create the db's (except sqlite) for you. You can look at .travis.yml before_script for an example of how to create the db's needed.
Though be aware we do not have the abilty to create the db's (except sqlite) for you. You can look at .travis.yml before_script for an example of how to create the db's needed.

```
export DB=postgres
Expand Down
11 changes: 11 additions & 0 deletions lib/paper_trail/frameworks/rspec.rb
Expand Up @@ -22,3 +22,14 @@
# check to see if the model has `has_paper_trail` declared on it
match { |actual| actual.kind_of?(::PaperTrail::Model::InstanceMethods) }
end

RSpec::Matchers.define :have_a_version_with do |attributes|
# check if the model has a version with the specified attributes
match do |actual|
mathing_version = actual.versions.select do |version|
object = version.object ? PaperTrail.serializer.load(version.object) : {}
(HashWithIndifferentAccess.new(attributes).to_a - object.to_a).empty?
end
mathing_version.present?
end
end
14 changes: 14 additions & 0 deletions spec/models/widget_spec.rb
Expand Up @@ -7,6 +7,20 @@

let(:widget) { Widget.create! :name => 'Bob', :an_integer => 1 }

describe '`have_a_version_with` matcher', :versioning => true do
before do
widget.update_attributes!(:name => 'Leonard', :an_integer => 1 )
widget.update_attributes!(:name => 'Tom')
widget.update_attributes!(:name => 'Bob')
end

it "is possible to do assertions on versions" do
widget.should have_a_version_with :name => 'Leonard', :an_integer => 1
widget.should have_a_version_with :an_integer => 1
widget.should have_a_version_with :name => 'Tom'
end
end

describe "`versioning` option" do
context :enabled, :versioning => true do
it 'should enable versioning for models wrapped within a block' do
Expand Down

0 comments on commit 80071ba

Please sign in to comment.