spec/models/widget_spec.rb in paper_trail-5.2.0 vs spec/models/widget_spec.rb in paper_trail-5.2.1
- old
+ new
@@ -138,11 +138,46 @@
end
end
describe "Methods" do
describe "Instance", versioning: true do
- describe '#paper_trail.originator' do
+ describe "#create" do
+ it "creates a version record" do
+ wordget = Widget.create
+ assert_equal 1, wordget.versions.length
+ end
+ end
+
+ describe "#destroy" do
+ it "creates a version record" do
+ widget = Widget.create
+ assert_equal 1, widget.versions.length
+ widget.destroy
+ versions_for_widget = PaperTrail::Version.with_item_keys("Widget", widget.id)
+ assert_equal 2, versions_for_widget.length
+ end
+
+ it "can have multiple destruction records" do
+ versions = lambda { |widget|
+ # Workaround for AR 3. When we drop AR 3 support, we can simply use
+ # the `widget.versions` association, instead of `with_item_keys`.
+ PaperTrail::Version.with_item_keys("Widget", widget.id)
+ }
+ widget = Widget.create
+ assert_equal 1, widget.versions.length
+ widget.destroy
+ assert_equal 2, versions.call(widget).length
+ widget = widget.version.reify
+ widget.save
+ assert_equal 3, versions.call(widget).length
+ widget.destroy
+ assert_equal 4, versions.call(widget).length
+ assert_equal 2, versions.call(widget).where(event: "destroy").length
+ end
+ end
+
+ describe "#paper_trail.originator" do
describe "return value" do
let(:orig_name) { FFaker::Name.name }
let(:new_name) { FFaker::Name.name }
before { PaperTrail.whodunnit = orig_name }
@@ -190,21 +225,21 @@
end
end
end
end
- describe '#version_at' do
+ describe "#version_at" do
context "Timestamp argument is AFTER object has been destroyed" do
it "should return `nil`" do
widget.update_attribute(:name, "foobar")
widget.destroy
expect(widget.paper_trail.version_at(Time.now)).to be_nil
end
end
end
- describe '#whodunnit' do
+ describe "#whodunnit" do
it { is_expected.to respond_to(:whodunnit) }
context "no block given" do
it "should raise an error" do
expect {
@@ -248,11 +283,11 @@
end
end
end
end
- describe '#touch_with_version' do
+ describe "#touch_with_version" do
it "creates a version" do
count = widget.versions.size
# Travel 1 second because MySQL lacks sub-second resolution
Timecop.travel(1) do
widget.paper_trail.touch_with_version
@@ -265,9 +300,18 @@
# Travel 1 second because MySQL lacks sub-second resolution
Timecop.travel(1) do
widget.paper_trail.touch_with_version
end
expect(widget.updated_at).to be > time_was
+ end
+ end
+
+ describe "#update" do
+ it "creates a version record" do
+ widget = Widget.create
+ assert_equal 1, widget.versions.length
+ widget.update_attributes(name: "Bugle")
+ assert_equal 2, widget.versions.length
end
end
end
describe "Class" do