Commit c0ccf5f09a1bf35fe26e177747a0e804669370b2
Committed by
Rafael Manzo
1 parent
f2063804
Exists in
colab
and in
4 other branches
Reading group entity.
Created model and its unit tests. Signed-off-by: Renan Fichberg <rfichberg@gmail.com>
Showing
5 changed files
with
98 additions
and
2 deletions
Show diff stats
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +require "validators/kalibro_uniqueness_validator.rb" | ||
2 | + | ||
3 | +class ReadingGroup < KalibroGem::Entities::ReadingGroup | ||
4 | + include KalibroRecord | ||
5 | + | ||
6 | + attr_accessor :name | ||
7 | + validates :name, presence: true, kalibro_uniqueness: true | ||
8 | + | ||
9 | + def readings | ||
10 | + Reading.readings_of(self.id) | ||
11 | + end | ||
12 | +end |
spec/factories/reading_groups.rb
1 | FactoryGirl.define do | 1 | FactoryGirl.define do |
2 | - factory :reading_group, class: KalibroGem::Entities::ReadingGroup do | 2 | + factory :reading_group do |
3 | id 1 | 3 | id 1 |
4 | name "Mussum" | 4 | name "Mussum" |
5 | description "Cacildis!" | 5 | description "Cacildis!" |
6 | end | 6 | end |
7 | -end | ||
8 | \ No newline at end of file | 7 | \ No newline at end of file |
8 | +end |
@@ -0,0 +1,75 @@ | @@ -0,0 +1,75 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe ReadingGroup do | ||
4 | + describe 'methods' do | ||
5 | + describe 'persisted?' do | ||
6 | + before :each do | ||
7 | + @subject = FactoryGirl.build(:reading_group) | ||
8 | + ReadingGroup.expects(:exists?).with(@subject.id).returns(false) | ||
9 | + end | ||
10 | + | ||
11 | + it 'should return false' do | ||
12 | + @subject.persisted?.should eq(false) | ||
13 | + end | ||
14 | + end | ||
15 | + | ||
16 | + describe 'update' do | ||
17 | + before :each do | ||
18 | + @qt = FactoryGirl.build(:reading_group) | ||
19 | + @qt_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers | ||
20 | + end | ||
21 | + | ||
22 | + context 'with valid attributes' do | ||
23 | + before :each do | ||
24 | + @qt.expects(:save).returns(true) | ||
25 | + end | ||
26 | + | ||
27 | + it 'should return true' do | ||
28 | + @qt.update(@qt_params).should eq(true) | ||
29 | + end | ||
30 | + end | ||
31 | + | ||
32 | + context 'with invalid attributes' do | ||
33 | + before :each do | ||
34 | + @qt.expects(:save).returns(false) | ||
35 | + end | ||
36 | + | ||
37 | + it 'should return false' do | ||
38 | + @qt.update(@qt_params).should eq(false) | ||
39 | + end | ||
40 | + end | ||
41 | + end | ||
42 | + | ||
43 | + describe 'readings' do | ||
44 | + subject { FactoryGirl.build(:reading_group) } | ||
45 | + let(:reading) { FactoryGirl.build(:reading) } | ||
46 | + | ||
47 | + it 'should call readings_of on the Reading model' do | ||
48 | + Reading.expects(:readings_of).with(subject.id).returns([reading]) | ||
49 | + | ||
50 | + subject.readings.should include(reading) | ||
51 | + end | ||
52 | + end | ||
53 | + end | ||
54 | + | ||
55 | + describe 'validations' do | ||
56 | + subject {FactoryGirl.build(:reading_group)} | ||
57 | + context 'active model validations' do | ||
58 | + before :each do | ||
59 | + ReadingGroup.expects(:all).at_least_once.returns([]) | ||
60 | + end | ||
61 | + it { should validate_presence_of(:name) } | ||
62 | + end | ||
63 | + | ||
64 | + context 'kalibro validations' do | ||
65 | + before :each do | ||
66 | + ReadingGroup.expects(:request).returns(42) | ||
67 | + end | ||
68 | + | ||
69 | + it 'should validate uniqueness' do | ||
70 | + KalibroUniquenessValidator.any_instance.expects(:validate_each).with(subject, :name, subject.name) | ||
71 | + subject.save | ||
72 | + end | ||
73 | + end | ||
74 | + end | ||
75 | +end |