All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gems.virtus-1.0.5.spec.integration.overriding_virtus_spec.rb Maven / Gradle / Ivy

The newest version!
require 'spec_helper'

describe 'overriding virtus behavior' do

  before do
    module Examples
      class Article
        include Virtus

        attribute :title, String

        def title
          super || ''
        end

        def title=(name)
          super unless self.title == "can't be changed"
        end
      end
    end
  end

  describe 'overriding an attribute getter' do
    specify 'calls the defined getter' do
      expect(Examples::Article.new.title).to eq('')
    end

    specify 'super can be used to access the getter defined by virtus' do
      expect(Examples::Article.new(:title => 'example article').title).to eq('example article')
    end
  end

  describe 'overriding an attribute setter' do
    specify 'calls the defined setter' do
      article = Examples::Article.new(:title => "can't be changed")
      article.title = 'this will never be assigned'
      expect(article.title).to eq("can't be changed")
    end

    specify 'super can be used to access the setter defined by virtus' do
      article = Examples::Article.new(:title => 'example article')
      article.title = 'my new title'
      expect(article.title).to eq('my new title')
    end
  end
end




© 2015 - 2024 Weber Informatics LLC | Privacy Policy