From f672063dd85571bd1ee4686f2a53c94f7622ac33 Mon Sep 17 00:00:00 2001 From: thiago Date: Mon, 27 Oct 2014 10:52:14 -0200 Subject: [PATCH] Fixed tests in project_controller.rb and closes #25 --- app/controllers/projects_controller.rb | 44 ++++++++++++++++++++------------------------ app/models/project.rb | 19 +++++++++---------- app/models/project_image.rb | 12 ++++++------ app/views/projects/_form.html.erb | 52 +++++++++++++++++++++++----------------------------- spec/controllers/projects_controller_spec.rb | 27 +++++++++++++++------------ spec/factories/project_images.rb | 12 +++++++++--- spec/models/project_image_spec.rb | 18 ++++++++++++++++++ 7 files changed, 100 insertions(+), 84 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index f6c6a8c..e7306d2 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -40,18 +40,14 @@ class ProjectsController < ApplicationController def edit set_project @project_image = ProjectImage.find_by_project_id(@project.id) - if !@project_image.nil? - @project_image.check_no_image - end + @project_image.check_no_image end def update set_project @project_image = ProjectImage.find_by_project_id(@project.id) - if !@project_image.nil? - @project_image.update(project_params[:image_url]) - end if @project.update(project_params) + @project_image.update(image_url: project_params[:image_url]) redirect_to(project_path(@project.id)) else render "edit" @@ -71,25 +67,25 @@ class ProjectsController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_project - @project = Project.find(params[:id]) - end + # Use callbacks to share common setup or constraints between actions. + def set_project + @project = Project.find(params[:id]) + end - # Never trust parameters from the scary internet, only allow the white list through. - def project_params - params[:project] - end + # Never trust parameters from the scary internet, only allow the white list through. + def project_params + params[:project] + end - # Extracted code from create action - def create_and_redir(format) - if @project.save - current_user.project_ownerships.create project_id: @project.id - format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' } - format.json { render action: 'show', status: :created, location: @project } - else - format.html { render action: 'new' } - format.json { render json: @project.errors, status: :unprocessable_entity } - end + # Extracted code from create action + def create_and_redir(format) + if @project.save + current_user.project_ownerships.create project_id: @project.id + format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' } + format.json { render action: 'show', status: :created, location: @project } + else + format.html { render action: 'new' } + format.json { render json: @project.errors, status: :unprocessable_entity } end + end end diff --git a/app/models/project.rb b/app/models/project.rb index a9b6d12..19aa9a1 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,17 +1,16 @@ require "validators/kalibro_uniqueness_validator.rb" class Project < KalibroGatekeeperClient::Entities::Project - include KalibroRecord + include KalibroRecord - attr_accessor :name,:image_url - validates :name, presence: true, kalibro_uniqueness: true + attr_accessor :name,:image_url + validates :name, presence: true, kalibro_uniqueness: true - def repositories - Repository.repositories_of(self.id) - end - - def self.latest(count = 1) - all.sort { |a,b| b.id <=> a.id }.first(count) - end + def repositories + Repository.repositories_of(self.id) + end + def self.latest(count = 1) + all.sort { |a,b| b.id <=> a.id }.first(count) + end end diff --git a/app/models/project_image.rb b/app/models/project_image.rb index ff83267..934181e 100644 --- a/app/models/project_image.rb +++ b/app/models/project_image.rb @@ -5,16 +5,16 @@ class ProjectImage < ActiveRecord::Base def check_no_image if !self.blank? - if self.image_url == "no-image-available.png" - self.image_url = "" - end + if self.image_url == "no-image-available.png" + self.image_url = "" + end end end def check_url - if self.image_url.blank? - self.image_url = "no-image-available.png" - end + if self.image_url.blank? + self.image_url = "no-image-available.png" + end end diff --git a/app/views/projects/_form.html.erb b/app/views/projects/_form.html.erb index 8edff67..f2462e8 100644 --- a/app/views/projects/_form.html.erb +++ b/app/views/projects/_form.html.erb @@ -1,34 +1,28 @@ <%= form_for(@project, :html => { role: 'form' }) do |f| %> <%= render :partial => 'shared/form_errors', :locals => {:object => @project} %> - -
-
-
-
- <%= f.label :name, class: 'control-label' %>
- <%= f.text_field :name, :required => true, class: 'text-field form-control' %> +
+
+
+
+ <%= f.label :name, class: 'control-label' %>
+ <%= f.text_field :name, :required => true, class: 'text-field form-control' %> +
+
+
+ <%= f.label :description, class: 'control-label' %>
+ <%= f.text_area :description, class: 'text-area form-control' %> +
+
+
+
+ <%= f.label "Image url", class: 'control-label' %>
+ <%= f.text_field :image_url, class: 'text-area' ,value: @project_image.image_url %> +
+
- -
-
- <%= f.label :description, class: 'control-label' %>
- <%= f.text_area :description, class: 'text-area form-control' %> -
-
- -
-
- <%= f.label "Image url", class: 'control-label' %>
- <%= f.text_field :image_url, class: 'text-area' ,value: @project_image.image_url %> -
+
+ <%= f.submit 'Save', class: 'btn btn-primary' %> + <%= link_to 'Back', projects_path, class: 'btn btn-default' %>
- - -
-
-
- <%= f.submit 'Save', class: 'btn btn-primary' %> - <%= link_to 'Back', projects_path, class: 'btn btn-default' %> -
-<% end %> + <% end %> diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index 68af865..d7bb0b1 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -18,10 +18,10 @@ describe ProjectsController, :type => :controller do end context 'with valid fields' do - let(:project) { FactoryGirl.build(:project) } - let(:subject_params) { Hash[FactoryGirl.attributes_for(:project).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 + let(:project) { FactoryGirl.build(:project) } + let(:subject_params) { Hash[FactoryGirl.attributes_for(:project).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 - before :each do + before :each do Project.any_instance.expects(:save).returns(true) end @@ -62,8 +62,8 @@ describe ProjectsController, :type => :controller do end describe 'show' do - subject { FactoryGirl.build(:project) } - let(:repository) { FactoryGirl.build(:repository) } + subject { FactoryGirl.build(:project) } + let(:repository) { FactoryGirl.build(:repository) } before :each do Project.expects(:find).with(subject.id.to_s).returns(subject) subject.expects(:repositories).returns(repository) @@ -116,7 +116,7 @@ describe ProjectsController, :type => :controller do delete :destroy, :id => @subject.id end - it { is_expected.to redirect_to(projects_path) } + it { is_expected.to redirect_to(projects_path) } end end @@ -142,6 +142,7 @@ describe ProjectsController, :type => :controller do describe 'edit' do before do @subject = FactoryGirl.build(:project) + @project_image = FactoryGirl.create(:project_image) end context 'with an User logged in' do @@ -149,7 +150,6 @@ describe ProjectsController, :type => :controller do @user = FactoryGirl.create(:user) @ownership = FactoryGirl.build(:project_ownership) @ownerships = [] - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) sign_in @user @@ -159,6 +159,7 @@ describe ProjectsController, :type => :controller do before :each do Project.expects(:find).with(@subject.id.to_s).returns(@subject) @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) + ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image) get :edit, :id => @subject.id end @@ -194,8 +195,10 @@ describe ProjectsController, :type => :controller do describe 'update' do before do - @subject = FactoryGirl.build(:project) - @subject_params = Hash[FactoryGirl.attributes_for(:project).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 + @project_image = FactoryGirl.create(:project_image) + @subject = FactoryGirl.create(:project) + @subject_params = Hash[FactoryGirl.attributes_for(:project).map { |k,v| [k.to_s, v.to_s] }] + end context 'when the user is logged in' do @@ -207,9 +210,10 @@ describe ProjectsController, :type => :controller do before do @ownership = FactoryGirl.build(:project_ownership) @ownerships = [] - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) + ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image) + end context 'with valid fields' do @@ -221,7 +225,6 @@ describe ProjectsController, :type => :controller do context 'rendering the show' do before :each do Project.expects(:exists?).returns(true) - post :update, :id => @subject.id, :project => @subject_params end @@ -241,7 +244,7 @@ describe ProjectsController, :type => :controller do context 'with an invalid field' do before :each do - Project.expects(:find).with(@subject.id.to_s).returns(@subject) + #Project.expects(:find).with(@subject.id.to_s).returns(@subject) Project.any_instance.expects(:update).with(@subject_params).returns(false) post :update, :id => @subject.id, :project => @subject_params diff --git a/spec/factories/project_images.rb b/spec/factories/project_images.rb index 5e9bb42..ce0ebe4 100644 --- a/spec/factories/project_images.rb +++ b/spec/factories/project_images.rb @@ -1,7 +1,13 @@ # Read about factories at https://github.com/thoughtbot/factory_girl FactoryGirl.define do - factory :project_image do - image_url "MyString" - end + factory :project_image do + project_id 1 + image_url "Example" + end + + factory :project_no_image, class: ProjectImage do + image_url nil + end + end diff --git a/spec/models/project_image_spec.rb b/spec/models/project_image_spec.rb index fa7cda4..a051029 100644 --- a/spec/models/project_image_spec.rb +++ b/spec/models/project_image_spec.rb @@ -3,5 +3,23 @@ require 'rails_helper' RSpec.describe ProjectImage, :type => :model do describe 'associations' do it { is_expected.to belong_to(:project) } + it { should belong_to(:project) } end + + describe 'methods' do + + it 'should validate url blank' do + @project_no_image = FactoryGirl.create(:project_no_image) + expect(@project_no_image.image_url).to eq("no-image-available.png") + end + + it "Can be assigns" do + expect(ProjectImage.new).to be_an_instance_of(ProjectImage) + end + + it 'should validate url field' do + @project_no_image = FactoryGirl.create(:project_no_image) + expect(@project_no_image.check_no_image).to eq("") + end + end end -- libgit2 0.21.2