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 matcher #394

Merged
merged 1 commit into from Jul 9, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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