add have_a_version_with matcher

This commit is contained in:
Emre Basala 2014-07-09 13:47:15 +01:00
parent b9b0d17768
commit a2f1730e52
3 changed files with 46 additions and 2 deletions

View File

@ -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.
@ -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

View File

@ -22,3 +22,14 @@ RSpec::Matchers.define :be_versioned do
# 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

View File

@ -7,6 +7,20 @@ describe Widget do
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