Sha256: 759ba06fee2e0ac2c1fc28822fc138abf9ee3afad83e53c41314327b85543e42

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

require 'helper'

describe Toy::Dirty do
  uses_objects('User')

  before do
    User.attribute(:name, String)
  end

  it "has no changes for new with no attributes" do
    User.new.should_not be_changed
    User.new.changed.should be_empty
    User.new.changes.should be_empty
  end

  it "has changes for new with attributes" do
    user = User.new(:name => 'Geoffrey')
    user.should be_changed
    user.changed.should include('name')
    user.changes.should == {'name' => [nil, 'Geoffrey']}
  end

  it "knows attribute changed through writer" do
    user = User.new
    user.name = 'John'
    user.should be_changed
    user.changed.should include('name')
    user.changes['name'].should == [nil, 'John']
  end

  it "knows when attribute did not change" do
    user = User.new
    user.name = nil
    user.should_not be_changed
  end

  it "has attribute changed? method" do
    user = User.new
    user.should_not be_name_changed
    user.name = 'John'
    user.should be_name_changed
  end

  it "has attribute was method" do
    user = User.new
    user.name = 'Steve'
    user.name_was.should be_nil
  end

  it "has attribute change method" do
    user = User.new
    user.name = 'Steve'
    user.name_change.should == [nil, 'Steve']
  end

  it "has attribute will change! method" do
    user = User.new
    user.name_will_change!
    user.should be_changed
  end

  describe "#clone" do
    it "has no changes" do
      User.new.clone.should_not be_changed
    end
  end

  # https://github.com/newtoy/toystore/issues/13
  describe "Overriding initialize and setting an attribute before calling super" do
    before do
      User.attribute(:name, String)
      User.class_eval do
        def initialize(*)
          self.name = 'John'
          super
        end
      end
    end

    it "does not throw error" do
      lambda { User.new }.should_not raise_error
    end

    it "sets value" do
      User.new.name.should == 'John'
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
toystore-0.13.2 spec/toy/dirty_spec.rb
toystore-0.13.1 spec/toy/dirty_spec.rb
toystore-0.13.0 spec/toy/dirty_spec.rb
toystore-0.12.0 spec/toy/dirty_spec.rb
toystore-0.11.0 spec/toy/dirty_spec.rb
toystore-0.10.4 spec/toy/dirty_spec.rb