Commit f9fabd2ee79a467d758f03e0da3f3c4f9f9c77b9
1 parent
22f07c1d
Exists in
colab
and in
4 other branches
Add repository setter to RepositoryAttributes with tests
Signed-off-by: Heitor Reis <marcheing@gmail.com>
Showing
3 changed files
with
33 additions
and
5 deletions
Show diff stats
app/models/repository_attributes.rb
... | ... | @@ -4,6 +4,11 @@ class RepositoryAttributes < ActiveRecord::Base |
4 | 4 | validates :user, presence: true |
5 | 5 | |
6 | 6 | def repository |
7 | - Repository.find(repository_id) | |
7 | + @repository ||= Repository.find(repository_id) | |
8 | + end | |
9 | + | |
10 | + def repository=(repository) | |
11 | + @repository = repository | |
12 | + self.repository_id = @repository ? @repository.id : nil | |
8 | 13 | end |
9 | 14 | end | ... | ... |
spec/factories/repository_attributes.rb
spec/models/repository_attributes_spec.rb
... | ... | @@ -12,12 +12,35 @@ RSpec.describe RepositoryAttributes, type: :model do |
12 | 12 | |
13 | 13 | describe 'methods' do |
14 | 14 | describe 'repository' do |
15 | + subject { FactoryGirl.build(:repository_attributes, repository: nil) } | |
16 | + let(:repository) { FactoryGirl.build(:repository) } | |
17 | + | |
18 | + before :each do | |
19 | + Repository.expects(:find).with(subject.repository_id).returns(repository) | |
20 | + end | |
21 | + | |
22 | + it 'should return the repository' do | |
23 | + expect(subject.repository).to eq(repository) | |
24 | + end | |
25 | + end | |
26 | + | |
27 | + describe 'repository=' do | |
15 | 28 | subject { FactoryGirl.build(:repository_attributes) } |
29 | + let(:repository) { FactoryGirl.build(:repository) } | |
16 | 30 | |
17 | - it 'is expected to find the repository by id' do | |
18 | - Repository.expects(:find).with(subject.repository_id).returns(subject) | |
31 | + context 'when the repository is not nil' do | |
32 | + it "should set the repository and it's ID correctly" do | |
33 | + subject.repository = repository | |
34 | + expect(subject.repository).to eq(repository) | |
35 | + expect(subject.repository_id).to eq(repository.id) | |
36 | + end | |
37 | + end | |
19 | 38 | |
20 | - subject.repository | |
39 | + context 'when the repository is nil' do | |
40 | + it "should unset the repository id" do | |
41 | + subject.repository = nil | |
42 | + expect(subject.repository_id).to be_nil | |
43 | + end | |
21 | 44 | end |
22 | 45 | end |
23 | 46 | end | ... | ... |