Commit 6ffae81b3992dee2c6e048734a19513db523ffde

Authored by Fellipe Souto
Committed by Rafael Manzo
1 parent 1d201125

Creating unit test for repositories_controller

Signed-off-by: João M. M Silva <jaodsilv@linux.ime.usp.br>
app/controllers/repositories_controller.rb
... ... @@ -21,8 +21,8 @@ class RepositoriesController &lt; ApplicationController
21 21  
22 22 # GET /repositories/1/edit
23 23 def edit
24   - @project_id = params[:project_id]
25   - set_repository
  24 + sproject_id = params[:project_id]
  25 + set_repository #fix me please
26 26 @repository_types = Repository.repository_types
27 27 end
28 28  
... ...
spec/controllers/repositories_controller_spec.rb
1 1 require 'spec_helper'
2 2  
3 3 describe RepositoriesController do
4   - describe 'show' do
5   - let(:project) { FactoryGirl.build(:project) }
6   - let(:processing) { FactoryGirl.build(:processing) }
7   - subject { FactoryGirl.build(:repository) }
  4 + #pending "add some examples to (or delete) #{__FILE__}"
  5 + project = FactoryGirl.build(:project)
  6 + describe 'new' do
  7 +
8 8  
9 9 before :each do
10   - subject.expects(:last_processing).returns(processing)
11   - Repository.expects(:find).at_least_once.with(subject.id).returns(subject)
12   - KalibroEntities::Entities::Configuration.expects(:find).with(subject.configuration_id) #FIXME: As soon as the Configuration model gets created refactor this!
  10 + sign_in FactoryGirl.create(:user)
  11 + get :new, project_id: project.id.to_s
  12 + end
  13 +
  14 + it { should respond_with(:success) }
  15 + it { should render_template(:new) }
  16 + end
  17 +
  18 + describe 'create' do
  19 + before do
  20 + sign_in FactoryGirl.create(:user)
  21 + @subject = FactoryGirl.build(:repository)
  22 + @subject_params = Hash[FactoryGirl.attributes_for(:repository).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with symbols and integers
  23 + end
  24 +
  25 + context 'with valid fields' do
  26 + before :each do
  27 + Repository.expects(:new).at_least_once.with(@subject_params).returns(@subject)
  28 + Repository.any_instance.expects(:save).returns(true)
  29 + Repository.any_instance.expects(:process)
  30 + Repository.any_instance.expects(:persisted?).returns(true).at_least_once
  31 + post :create, project_id: project.id.to_s, repository: @subject_params
  32 + end
  33 +
  34 + it 'should redirect to the show view' do
  35 + response.should redirect_to project_path(@subject)
  36 + end
  37 + it 'should respond a redirect' do
  38 + should respond_with(:redirect)
  39 + end
  40 + end
13 41  
14   - get :show, project_id: project.id, id: subject.id
  42 + context 'with an invalid field' do
  43 + before :each do
  44 + Repository.expects(:new).at_least_once.with(@subject_params).returns(@subject)
  45 + Repository.any_instance.expects(:save).returns(false)
  46 +
  47 + post :create, project_id: project.id.to_s, repository: @subject_params
  48 + end
  49 +
  50 + it { should render_template(:new) }
  51 + end
  52 + end
  53 +
  54 + describe 'show' do
  55 + before :each do
  56 + @subject = FactoryGirl.build(:repository)
  57 + Repository.expects(:find).with(@subject.id).returns(@subject)
  58 + get :show, id: @subject.id.to_s, project_id: project.id.to_s
15 59 end
16 60  
17 61 it { should render_template(:show) }
18 62 end
  63 +#To do
  64 +
  65 + # describe 'destroy' do
  66 + # before do
  67 + # @subject = FactoryGirl.build(:repository)
  68 + # end
  69 +
  70 + # context 'with an User logged in' do
  71 + # before do
  72 + # sign_in FactoryGirl.create(:user)
  73 + # @ownership = FactoryGirl.build(:project_ownership)
  74 + # @ownerships = []
  75 +
  76 + # end
  77 +
  78 + # context 'when the user owns the repository' do
  79 + # before :each do
  80 + # @ownership.expects(:destroy)
  81 + # @subject.expects(:destroy)
  82 +
  83 + # #Those two mocks looks the same but they are necessary since params[:id] is a String and @repository.id is an Integer :(
  84 + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(@ownership)
  85 + # @ownerships.expects(:find_by_repository_id).with(@subject.id).returns(@ownership)
  86 +
  87 + # User.any_instance.expects(:repository_ownerships).at_least_once.returns(@ownerships)
  88 +
  89 + # Repository.expects(:find).with(@subject.id.to_s).returns(@subject)
  90 + # delete :destroy, id: @subject.id, project_id: project.id.to_s
  91 + # end
  92 +
  93 + # it 'should redirect to the repositories page' do
  94 + # response.should redirect_to repositories_url
  95 + # end
  96 +
  97 + # it { should respond_with(:redirect) }
  98 + # end
  99 +
  100 + # context "when the user doesn't own the repository" do
  101 + # before :each do
  102 + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(nil)
  103 + # User.any_instance.expects(:repository_ownerships).at_least_once.returns(@ownerships)
  104 +
  105 + # delete :destroy, :id => @subject.id
  106 + # end
  107 +
  108 + # it { should redirect_to(repositories_path) }
  109 + # end
  110 + end
  111 +
  112 + # context 'with no User logged in' do
  113 + # before :each do
  114 + # delete :destroy, :id => @subject.id
  115 + # end
  116 +
  117 + # it { should redirect_to new_user_session_path }
  118 + # end
  119 + end
  120 +
  121 + # describe 'index' do
  122 + # before :each do
  123 + # @subject = FactoryGirl.build(:repository)
  124 + # Repository.expects(:all).returns([@subject])
  125 + # get :index
  126 + # end
  127 +
  128 + # it { should render_template(:index) }
  129 + # end
  130 +
  131 + # describe 'edit' do
  132 + # before do
  133 + # @subject = FactoryGirl.build(:repository)
  134 + # end
  135 +
  136 + # context 'with an User logged in' do
  137 + # before do
  138 + # @user = FactoryGirl.create(:user)
  139 + # @ownership = FactoryGirl.build(:repository_ownership)
  140 + # @ownerships = []
  141 +
  142 + # User.any_instance.expects(:repository_ownerships).at_least_once.returns(@ownerships)
  143 +
  144 + # sign_in @user
  145 + # end
  146 +
  147 + # context 'when the user owns the repository' do
  148 + # before :each do
  149 + # Repository.expects(:find).with(@subject.id.to_s).returns(@subject)
  150 + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(@ownership)
  151 +
  152 + # get :edit, :id => @subject.id
  153 + # end
  154 +
  155 + # it { should render_template(:edit) }
  156 +
  157 + # it 'should assign to @repository the @subject' do
  158 + # assigns(:repository).should eq(@subject)
  159 + # end
  160 + # end
  161 +
  162 + # context 'when the user does not own the repository' do
  163 + # before do
  164 + # @subject = FactoryGirl.build(:another_repository)
  165 + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(nil)
  166 +
  167 + # get :edit, :id => @subject.id
  168 + # end
  169 +
  170 + # it { should redirect_to(repositories_path) }
  171 +
  172 + # it 'should set the flash' do
  173 + # pending("This ShouldaMatcher test is not compatible yet with Rails 4") do
  174 + # should set_the_flash[:notice].to("You shall not edit repositories that aren't yours.")
  175 + # end
  176 + # end
  177 + # end
  178 + # end
  179 +
  180 + # context 'with no user logged in' do
  181 + # before :each do
  182 + # get :edit, :id => @subject.id
  183 + # end
  184 +
  185 + # it { should redirect_to new_user_session_path }
  186 + # end
  187 + # end
  188 +
  189 + # describe 'update' do
  190 + # before do
  191 + # @subject = FactoryGirl.build(:repository)
  192 + # @subject_params = Hash[FactoryGirl.attributes_for(:repository).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
  193 + # end
  194 +
  195 + # context 'when the user is logged in' do
  196 + # before do
  197 + # sign_in FactoryGirl.create(:user)
  198 + # end
  199 +
  200 + # context 'when user owns the repository' do
  201 + # before do
  202 + # @ownership = FactoryGirl.build(:repository_ownership)
  203 + # @ownerships = []
  204 +
  205 + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(@ownership)
  206 + # User.any_instance.expects(:repository_ownerships).at_least_once.returns(@ownerships)
  207 + # end
  208 +
  209 + # context 'with valid fields' do
  210 + # before :each do
  211 + # Repository.expects(:find).with(@subject.id.to_s).returns(@subject)
  212 + # Repository.any_instance.expects(:update).with(@subject_params).returns(true)
  213 + # end
  214 +
  215 + # context 'rendering the show' do
  216 + # before :each do
  217 + # Repository.expects(:exists?).returns(true)
  218 +
  219 + # post :update, :id => @subject.id, :repository => @subject_params
  220 + # end
  221 +
  222 + # it 'should redirect to the show view' do
  223 + # response.should redirect_to repository_path(@subject)
  224 + # end
  225 + # end
  226 +
  227 + # context 'without rendering the show view' do
  228 + # before :each do
  229 + # post :update, :id => @subject.id, :repository => @subject_params
  230 + # end
  231 +
  232 + # it { should respond_with(:redirect) }
  233 + # end
  234 + # end
  235 +
  236 + # context 'with an invalid field' do
  237 + # before :each do
  238 + # Repository.expects(:find).with(@subject.id.to_s).returns(@subject)
  239 + # Repository.any_instance.expects(:update).with(@subject_params).returns(false)
  240 +
  241 + # post :update, :id => @subject.id, :repository => @subject_params
  242 + # end
  243 +
  244 + # it { should render_template(:edit) }
  245 + # end
  246 + # end
  247 +
  248 + # context 'when the user does not own the repository' do
  249 + # before :each do
  250 + # post :update, :id => @subject.id, :repository => @subject_params
  251 + # end
  252 +
  253 + # it { should redirect_to repositories_path }
  254 + # end
  255 + # end
  256 +
  257 + # context 'with no user logged in' do
  258 + # before :each do
  259 + # post :update, :id => @subject.id, :repository => @subject_params
  260 + # end
  261 +
  262 + # it { should redirect_to new_user_session_path }
  263 + # end
  264 + # end
19 265 end
... ...
spec/factories/repositories.rb
  1 +# This file is part of KalibroEntities
  2 +# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file)
  3 +#
  4 +# This program is free software: you can redistribute it and/or modify
  5 +# it under the terms of the GNU General Public License as published by
  6 +# the Free Software Foundation, either version 3 of the License, or
  7 +# (at your option) any later version.
  8 +#
  9 +# This program is distributed in the hope that it will be useful,
  10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +# GNU General Public License for more details.
  13 +
  14 +# You should have received a copy of the GNU General Public License
  15 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
1 16 FactoryGirl.define do
2 17 factory :repository, class: Repository do
3 18 id 1
... ... @@ -12,7 +27,7 @@ FactoryGirl.define do
12 27 send_email "test@test.com"
13 28 end
14 29  
15   - factory :another_repository, class: Repository, parent: :repository do
  30 + factory :another_repository, parent: :repository do
16 31 id 2
17 32 end
18 33 end
19 34 \ No newline at end of file
... ...