Convert unit/inheritance_column_test to rspec

This commit is contained in:
Jared Beck 2017-02-05 19:08:27 -05:00
parent 01041df841
commit 379d9187e9
2 changed files with 50 additions and 66 deletions

View File

@ -1,35 +1,60 @@
require "rails_helper"
describe Animal, type: :model do
it { is_expected.to be_versioned }
describe Animal, type: :model, versioning: true do
it "baseline test setup" do
expect(Animal.new).to be_versioned
expect(Animal.inheritance_column).to eq("species")
end
describe "STI", versioning: true do
it { expect(Animal.inheritance_column).to eq("species") }
describe "updates to the `inheritance_column`" do
subject { Cat.create!(name: "Leo") }
it "should be allowed" do
subject.update_attributes(name: "Spike", species: "Dog")
dog = Animal.find(subject.id)
expect(dog).to be_instance_of(Dog)
end
it "works with custom STI inheritance column" do
animal = Animal.create(name: "Animal")
animal.update_attributes(name: "Animal from the Muppets")
animal.update_attributes(name: "Animal Muppet")
animal.destroy
dog = Dog.create(name: "Snoopy")
dog.update_attributes(name: "Scooby")
dog.update_attributes(name: "Scooby Doo")
dog.destroy
cat = Cat.create(name: "Garfield")
cat.update_attributes(name: "Garfield (I hate Mondays)")
cat.update_attributes(name: "Garfield The Cat")
cat.destroy
expect(PaperTrail::Version.count).to(eq(12))
expect(animal.versions.count).to(eq(4))
expect(animal.versions.first.reify).to(be_nil)
animal.versions[(1..-1)].each do |v|
expect(v.reify.class.name).to(eq("Animal"))
end
dog_versions = PaperTrail::Version.where(item_id: dog.id).order(:created_at)
expect(dog_versions.count).to(eq(4))
expect(dog_versions.first.reify).to(be_nil)
expect(dog_versions.map { |v| v.reify.class.name }).to eq(%w(NilClass Dog Dog Dog))
cat_versions = PaperTrail::Version.where(item_id: cat.id).order(:created_at)
expect(cat_versions.count).to(eq(4))
expect(cat_versions.first.reify).to(be_nil)
expect(cat_versions.map { |v| v.reify.class.name }).to eq(%w(NilClass Cat Cat Cat))
end
context "with callback-methods" do
context "when only has_paper_trail set in super class" do
let(:callback_cat) { Cat.create(name: "Markus") }
it "allows the inheritance_column (species) to be updated" do
cat = Cat.create!(name: "Leo")
cat.update_attributes(name: "Spike", species: "Dog")
dog = Animal.find(cat.id)
expect(dog).to be_instance_of(Dog)
end
it "trails all events" do
callback_cat.update_attributes(name: "Billie")
callback_cat.destroy
expect(callback_cat.versions.collect(&:event)).to eq %w(create update destroy)
end
context "with callback-methods" do
context "when only has_paper_trail set in super class" do
let(:callback_cat) { Cat.create(name: "Markus") }
it "does not break reify" do
callback_cat.destroy
expect { callback_cat.versions.last.reify }.not_to raise_error
end
it "trails all events" do
callback_cat.update_attributes(name: "Billie")
callback_cat.destroy
expect(callback_cat.versions.collect(&:event)).to eq %w(create update destroy)
end
it "does not break reify" do
callback_cat.destroy
expect { callback_cat.versions.last.reify }.not_to raise_error
end
end
end

View File

@ -1,41 +0,0 @@
require "test_helper"
class InheritanceColumnTest < ActiveSupport::TestCase
context "STI models" do
setup do
@animal = Animal.create name: "Animal"
@animal.update_attributes name: "Animal from the Muppets"
@animal.update_attributes name: "Animal Muppet"
@animal.destroy
@dog = Dog.create name: "Snoopy"
@dog.update_attributes name: "Scooby"
@dog.update_attributes name: "Scooby Doo"
@dog.destroy
@cat = Cat.create name: "Garfield"
@cat.update_attributes name: "Garfield (I hate Mondays)"
@cat.update_attributes name: "Garfield The Cat"
@cat.destroy
end
should "work with custom STI inheritance column" do
assert_equal 12, PaperTrail::Version.count
assert_equal 4, @animal.versions.count
assert_nil @animal.versions.first.reify
@animal.versions[1..-1].each { |v| assert_equal "Animal", v.reify.class.name }
# For some reason `@dog.versions` doesn't include the final `destroy` version.
# Neither do `@dog.versions.scoped` nor `@dog.versions(true)` nor `@dog.versions.reload`.
dog_versions = PaperTrail::Version.where(item_id: @dog.id).order(:created_at)
assert_equal 4, dog_versions.count
assert_nil dog_versions.first.reify
assert_equal %w(NilClass Dog Dog Dog), dog_versions.map { |v| v.reify.class.name }
cat_versions = PaperTrail::Version.where(item_id: @cat.id).order(:created_at)
assert_equal 4, cat_versions.count
assert_nil cat_versions.first.reify
assert_equal %w(NilClass Cat Cat Cat), cat_versions.map { |v| v.reify.class.name }
end
end
end