Sha256: a2b97232b65e1e004cf154073bb50b619f10d8fe4fa8594d8d4a416fe311aaeb

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

require "spec_helper"
require "byebug"

describe HasCalculatedFields do
  describe "runs before_save callbacks" do
    let(:name) { "name" }
    let(:created_at) { Time.find_zone("Madrid").parse("2019-05-03 2pm") }
    let(:obj) { create(:sample_model, created_at: created_at, name: name) }

    context "using time" do
      context "when attribute has value" do
        it "assigns formatted time attribute" do
          new_date = Time.find_zone("Madrid").parse("2019-05-04 4pm")

          obj.created_at = new_date
          expect { obj.save }.to change { obj.calculated_created_at }
            .from("Fri, 03 May 2019 14:00:00 +0200")
            .to("Sat, 04 May 2019 16:00:00 +0200")
        end
      end

      context "when attribute hasn't value" do
        let(:created_at) { nil }
        it "assigns formatted time attribute" do
          expect { obj.save }.to change { obj.calculated_created_at }
            .from(I18n.l(Time.current))
            .to(I18n.l(Time.current.in_time_zone("UTC")))
        end
      end
    end

    context "using method" do
      it "assigns new value to the calculated attribute" do
        obj.name = "new name"

        expect { obj.save }.to change { obj.calculated_name }
          .from("name calculated!")
          .to("new name calculated!")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
has_calculated_fields-1.0.3.2 spec/has_calculated_fields_spec.rb
has_calculated_fields-1.0.3.1 spec/has_calculated_fields_spec.rb
has_calculated_fields-1.0.3 spec/has_calculated_fields_spec.rb
has_calculated_fields-1.0.2 spec/has_calculated_fields_spec.rb
has_calculated_fields-1.0.1 spec/has_calculated_fields_spec.rb
has_calculated_fields-1.0.0 spec/has_calculated_fields_spec.rb