require "spec_helper" module Uuids::Models describe Uuid do describe "#value" do before { subject.record = create(:record) } it "can be assigned manually" do value = SecureRandom.uuid subject.value = value subject.save! expect(subject.reload.value).to eq value end it "assigned by default" do expect(subject.value).not_to be_blank end end describe "#value=" do let!(:subject) { build :uuid } let!(:value) { SecureRandom.uuid } it "sets #value before creation" do subject.value = value subject.save! expect { subject.reload }.not_to change { subject.value }.from value end it "doesn't change #value after creation" do subject.save! expect { subject.update_attributes! value: value } .not_to change { subject.reload } end end describe "#record=" do it "sets a #record" do record = create :record expect { subject.record = record } .to change { subject.record }.to record end end describe "#valid?" do subject { build :uuid } before { expect(subject).to be_valid } it "fails without a #record" do subject.record = nil expect(subject).not_to be_valid end it "fails with wrong value" do subject.value = "not a valid uuid" expect(subject).not_to be_valid end end describe "#destroy!" do subject { create :uuid } it "is forbidden" do expect { subject.destroy! }.to raise_error end it "adds an error messages" do expect { subject.destroy }.to change { subject.errors.blank? }.to false end end end end