2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
require 'has_errors'
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe HasErrors do
|
2015-03-26 16:57:50 -04:00
|
|
|
class ErrorTestClass
|
|
|
|
include HasErrors
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:error_test) { ErrorTestClass.new }
|
|
|
|
let(:title_error) { "Title can't be blank" }
|
|
|
|
|
|
|
|
# No title is invalid
|
|
|
|
let(:invalid_topic) { Fabricate.build(:topic, title: '') }
|
|
|
|
|
|
|
|
it "has no errors by default" do
|
|
|
|
expect(error_test.errors).to be_blank
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "validate_child" do
|
2015-03-26 16:57:50 -04:00
|
|
|
it "adds the errors from invalid AR objects" do
|
|
|
|
expect(error_test.validate_child(invalid_topic)).to eq(false)
|
|
|
|
expect(error_test.errors).to be_present
|
|
|
|
expect(error_test.errors[:base]).to include(title_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add the errors from valid AR objects" do
|
|
|
|
topic = Fabricate.build(:topic)
|
|
|
|
expect(error_test.validate_child(topic)).to eq(true)
|
|
|
|
expect(error_test.errors).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "rollback_from_errors!" do
|
2015-03-26 16:57:50 -04:00
|
|
|
it "triggers a rollback" do
|
|
|
|
invalid_topic.valid?
|
|
|
|
|
2022-02-25 21:51:39 -05:00
|
|
|
expect { error_test.rollback_from_errors!(invalid_topic) }.to raise_error(ActiveRecord::Rollback)
|
2015-03-26 16:57:50 -04:00
|
|
|
expect(error_test.errors).to be_present
|
|
|
|
expect(error_test.errors[:base]).to include(title_error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "rollback_with_error!" do
|
2015-03-26 16:57:50 -04:00
|
|
|
it "triggers a rollback" do
|
|
|
|
|
2022-02-25 21:51:39 -05:00
|
|
|
expect do
|
2016-05-05 08:58:25 -04:00
|
|
|
error_test.rollback_with!(invalid_topic, :too_many_users)
|
2022-02-25 21:51:39 -05:00
|
|
|
end.to raise_error(ActiveRecord::Rollback)
|
2015-03-26 16:57:50 -04:00
|
|
|
expect(error_test.errors).to be_present
|
2016-05-05 08:58:25 -04:00
|
|
|
expect(error_test.errors[:base]).to include("You can only send warnings to one user at a time.")
|
2015-03-26 16:57:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|