From 6ffae81b3992dee2c6e048734a19513db523ffde Mon Sep 17 00:00:00 2001 From: Fellipe Souto Date: Mon, 21 Oct 2013 18:38:15 -0200 Subject: [PATCH] Creating unit test for repositories_controller --- app/controllers/repositories_controller.rb | 4 ++-- spec/controllers/repositories_controller_spec.rb | 262 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- spec/factories/repositories.rb | 17 ++++++++++++++++- 3 files changed, 272 insertions(+), 11 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 759f82a..e2c2deb 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -21,8 +21,8 @@ class RepositoriesController < ApplicationController # GET /repositories/1/edit def edit - @project_id = params[:project_id] - set_repository + sproject_id = params[:project_id] + set_repository #fix me please @repository_types = Repository.repository_types end diff --git a/spec/controllers/repositories_controller_spec.rb b/spec/controllers/repositories_controller_spec.rb index 390f4d8..8e6fa29 100644 --- a/spec/controllers/repositories_controller_spec.rb +++ b/spec/controllers/repositories_controller_spec.rb @@ -1,19 +1,265 @@ require 'spec_helper' describe RepositoriesController do - describe 'show' do - let(:project) { FactoryGirl.build(:project) } - let(:processing) { FactoryGirl.build(:processing) } - subject { FactoryGirl.build(:repository) } + #pending "add some examples to (or delete) #{__FILE__}" + project = FactoryGirl.build(:project) + describe 'new' do + before :each do - subject.expects(:last_processing).returns(processing) - Repository.expects(:find).at_least_once.with(subject.id).returns(subject) - KalibroEntities::Entities::Configuration.expects(:find).with(subject.configuration_id) #FIXME: As soon as the Configuration model gets created refactor this! + sign_in FactoryGirl.create(:user) + get :new, project_id: project.id.to_s + end + + it { should respond_with(:success) } + it { should render_template(:new) } + end + + describe 'create' do + before do + sign_in FactoryGirl.create(:user) + @subject = FactoryGirl.build(:repository) + @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 + end + + context 'with valid fields' do + before :each do + Repository.expects(:new).at_least_once.with(@subject_params).returns(@subject) + Repository.any_instance.expects(:save).returns(true) + Repository.any_instance.expects(:process) + Repository.any_instance.expects(:persisted?).returns(true).at_least_once + post :create, project_id: project.id.to_s, repository: @subject_params + end + + it 'should redirect to the show view' do + response.should redirect_to project_path(@subject) + end + it 'should respond a redirect' do + should respond_with(:redirect) + end + end - get :show, project_id: project.id, id: subject.id + context 'with an invalid field' do + before :each do + Repository.expects(:new).at_least_once.with(@subject_params).returns(@subject) + Repository.any_instance.expects(:save).returns(false) + + post :create, project_id: project.id.to_s, repository: @subject_params + end + + it { should render_template(:new) } + end + end + + describe 'show' do + before :each do + @subject = FactoryGirl.build(:repository) + Repository.expects(:find).with(@subject.id).returns(@subject) + get :show, id: @subject.id.to_s, project_id: project.id.to_s end it { should render_template(:show) } end +#To do + + # describe 'destroy' do + # before do + # @subject = FactoryGirl.build(:repository) + # end + + # context 'with an User logged in' do + # before do + # sign_in FactoryGirl.create(:user) + # @ownership = FactoryGirl.build(:project_ownership) + # @ownerships = [] + + # end + + # context 'when the user owns the repository' do + # before :each do + # @ownership.expects(:destroy) + # @subject.expects(:destroy) + + # #Those two mocks looks the same but they are necessary since params[:id] is a String and @repository.id is an Integer :( + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(@ownership) + # @ownerships.expects(:find_by_repository_id).with(@subject.id).returns(@ownership) + + # User.any_instance.expects(:repository_ownerships).at_least_once.returns(@ownerships) + + # Repository.expects(:find).with(@subject.id.to_s).returns(@subject) + # delete :destroy, id: @subject.id, project_id: project.id.to_s + # end + + # it 'should redirect to the repositories page' do + # response.should redirect_to repositories_url + # end + + # it { should respond_with(:redirect) } + # end + + # context "when the user doesn't own the repository" do + # before :each do + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(nil) + # User.any_instance.expects(:repository_ownerships).at_least_once.returns(@ownerships) + + # delete :destroy, :id => @subject.id + # end + + # it { should redirect_to(repositories_path) } + # end + end + + # context 'with no User logged in' do + # before :each do + # delete :destroy, :id => @subject.id + # end + + # it { should redirect_to new_user_session_path } + # end + end + + # describe 'index' do + # before :each do + # @subject = FactoryGirl.build(:repository) + # Repository.expects(:all).returns([@subject]) + # get :index + # end + + # it { should render_template(:index) } + # end + + # describe 'edit' do + # before do + # @subject = FactoryGirl.build(:repository) + # end + + # context 'with an User logged in' do + # before do + # @user = FactoryGirl.create(:user) + # @ownership = FactoryGirl.build(:repository_ownership) + # @ownerships = [] + + # User.any_instance.expects(:repository_ownerships).at_least_once.returns(@ownerships) + + # sign_in @user + # end + + # context 'when the user owns the repository' do + # before :each do + # Repository.expects(:find).with(@subject.id.to_s).returns(@subject) + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(@ownership) + + # get :edit, :id => @subject.id + # end + + # it { should render_template(:edit) } + + # it 'should assign to @repository the @subject' do + # assigns(:repository).should eq(@subject) + # end + # end + + # context 'when the user does not own the repository' do + # before do + # @subject = FactoryGirl.build(:another_repository) + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(nil) + + # get :edit, :id => @subject.id + # end + + # it { should redirect_to(repositories_path) } + + # it 'should set the flash' do + # pending("This ShouldaMatcher test is not compatible yet with Rails 4") do + # should set_the_flash[:notice].to("You shall not edit repositories that aren't yours.") + # end + # end + # end + # end + + # context 'with no user logged in' do + # before :each do + # get :edit, :id => @subject.id + # end + + # it { should redirect_to new_user_session_path } + # end + # end + + # describe 'update' do + # before do + # @subject = FactoryGirl.build(:repository) + # @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 + # end + + # context 'when the user is logged in' do + # before do + # sign_in FactoryGirl.create(:user) + # end + + # context 'when user owns the repository' do + # before do + # @ownership = FactoryGirl.build(:repository_ownership) + # @ownerships = [] + + # @ownerships.expects(:find_by_repository_id).with("#{@subject.id}").returns(@ownership) + # User.any_instance.expects(:repository_ownerships).at_least_once.returns(@ownerships) + # end + + # context 'with valid fields' do + # before :each do + # Repository.expects(:find).with(@subject.id.to_s).returns(@subject) + # Repository.any_instance.expects(:update).with(@subject_params).returns(true) + # end + + # context 'rendering the show' do + # before :each do + # Repository.expects(:exists?).returns(true) + + # post :update, :id => @subject.id, :repository => @subject_params + # end + + # it 'should redirect to the show view' do + # response.should redirect_to repository_path(@subject) + # end + # end + + # context 'without rendering the show view' do + # before :each do + # post :update, :id => @subject.id, :repository => @subject_params + # end + + # it { should respond_with(:redirect) } + # end + # end + + # context 'with an invalid field' do + # before :each do + # Repository.expects(:find).with(@subject.id.to_s).returns(@subject) + # Repository.any_instance.expects(:update).with(@subject_params).returns(false) + + # post :update, :id => @subject.id, :repository => @subject_params + # end + + # it { should render_template(:edit) } + # end + # end + + # context 'when the user does not own the repository' do + # before :each do + # post :update, :id => @subject.id, :repository => @subject_params + # end + + # it { should redirect_to repositories_path } + # end + # end + + # context 'with no user logged in' do + # before :each do + # post :update, :id => @subject.id, :repository => @subject_params + # end + + # it { should redirect_to new_user_session_path } + # end + # end end diff --git a/spec/factories/repositories.rb b/spec/factories/repositories.rb index 1c68d03..06b9614 100644 --- a/spec/factories/repositories.rb +++ b/spec/factories/repositories.rb @@ -1,3 +1,18 @@ +# This file is part of KalibroEntities +# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . FactoryGirl.define do factory :repository, class: Repository do id 1 @@ -12,7 +27,7 @@ FactoryGirl.define do send_email "test@test.com" end - factory :another_repository, class: Repository, parent: :repository do + factory :another_repository, parent: :repository do id 2 end end \ No newline at end of file -- libgit2 0.21.2