File: mass_assignment_with_accessors_spec.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,036 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'spec_helper'

describe "mass assignment with accessors" do

  before do
    module Examples
      class Product
        include Virtus

        attribute :id,          Integer
        attribute :category,    String
        attribute :subcategory, String

        def categories=(categories)
          self.category = categories.first
          self.subcategory = categories.last
        end

      private

        def _id=(value)
          self.id = value
        end
      end
    end
  end

  subject { Examples::Product.new(:categories => ['Office', 'Printers'], :_id => 100) }

  specify 'works uppon instantiation' do
    expect(subject.category).to eq('Office')
    expect(subject.subcategory).to eq('Printers')
  end

  specify 'can be set with #attributes=' do
    subject.attributes = {:categories => ['Home', 'Furniture']}
    expect(subject.category).to eq('Home')
    expect(subject.subcategory).to eq('Furniture')
  end

  specify 'respects accessor visibility' do
    expect(subject.id).not_to eq(100)
  end
end