From 2f2ddebbea79702bc566609a8d03be4ba1588ef8 Mon Sep 17 00:00:00 2001 From: Rafael Reggiani Manzo Date: Mon, 17 Nov 2014 16:07:20 -0200 Subject: [PATCH] MezuroConfiguration using KalibroClient class --- app/models/mezuro_configuration.rb | 13 +------------ spec/controllers/mezuro_configurations_controller_spec.rb | 36 +++++++++++++++--------------------- spec/models/mezuro_configuration_spec.rb | 73 ------------------------------------------------------------------------- 3 files changed, 16 insertions(+), 106 deletions(-) delete mode 100644 spec/models/mezuro_configuration_spec.rb diff --git a/app/models/mezuro_configuration.rb b/app/models/mezuro_configuration.rb index c3dbb45..781e4f2 100644 --- a/app/models/mezuro_configuration.rb +++ b/app/models/mezuro_configuration.rb @@ -1,12 +1 @@ -require "validators/kalibro_uniqueness_validator.rb" - -class MezuroConfiguration < KalibroGatekeeperClient::Entities::Configuration - include KalibroRecord - - attr_accessor :name - validates :name, presence: true, kalibro_uniqueness: true - - def metric_configurations - MetricConfiguration.metric_configurations_of(self.id) - end -end +class MezuroConfiguration < KalibroClient::Configurations::KalibroConfiguration; end diff --git a/spec/controllers/mezuro_configurations_controller_spec.rb b/spec/controllers/mezuro_configurations_controller_spec.rb index 1746b8b..f819cc5 100644 --- a/spec/controllers/mezuro_configurations_controller_spec.rb +++ b/spec/controllers/mezuro_configurations_controller_spec.rb @@ -27,13 +27,11 @@ describe MezuroConfigurationsController, :type => :controller do context 'rendering the show' do before :each do - MezuroConfiguration.expects(:exists?).returns(true) - post :create, :mezuro_configuration => subject_params end it 'should redirect to the show view' do - expect(response).to redirect_to mezuro_configuration_path(mezuro_configuration) + expect(response).to redirect_to mezuro_configuration_path(mezuro_configuration.id) end end @@ -194,10 +192,8 @@ describe MezuroConfigurationsController, :type => :controller do end describe 'update' do - before do - @subject = FactoryGirl.build(:mezuro_configuration) - @subject_params = Hash[FactoryGirl.attributes_for(:mezuro_configuration).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 + let(:mezuro_configuration) {FactoryGirl.build(:mezuro_configuration)} + let(:mezuro_configuration_params) {Hash[FactoryGirl.attributes_for(:mezuro_configuration).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 context 'when the user is logged in' do before do @@ -209,31 +205,29 @@ describe MezuroConfigurationsController, :type => :controller do @ownership = FactoryGirl.build(:mezuro_configuration_ownership) @ownerships = [] - @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(@ownership) + @ownerships.expects(:find_by_mezuro_configuration_id).with("#{mezuro_configuration.id}").returns(@ownership) User.any_instance.expects(:mezuro_configuration_ownerships).at_least_once.returns(@ownerships) end context 'with valid fields' do before :each do - subject.expects(:find_resource).with(MezuroConfiguration, @subject.id).returns(@subject) - MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(true) + subject.expects(:find_resource).with(MezuroConfiguration, mezuro_configuration.id).returns(mezuro_configuration) + MezuroConfiguration.any_instance.expects(:update).with(mezuro_configuration_params).returns(true) end context 'rendering the show' do before :each do - MezuroConfiguration.expects(:exists?).returns(true) - - post :update, :id => @subject.id, :mezuro_configuration => @subject_params + post :update, :id => mezuro_configuration.id, :mezuro_configuration => mezuro_configuration_params end it 'should redirect to the show view' do - expect(response).to redirect_to mezuro_configuration_path(@subject) + expect(response).to redirect_to mezuro_configuration_path(mezuro_configuration.id) end end context 'without rendering the show view' do before :each do - post :update, :id => @subject.id, :mezuro_configuration => @subject_params + post :update, :id => mezuro_configuration.id, :mezuro_configuration => mezuro_configuration_params end it { is_expected.to respond_with(:redirect) } @@ -242,10 +236,10 @@ describe MezuroConfigurationsController, :type => :controller do context 'with an invalid field' do before :each do - subject.expects(:find_resource).with(MezuroConfiguration, @subject.id).returns(@subject) - MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(false) + subject.expects(:find_resource).with(MezuroConfiguration, mezuro_configuration.id).returns(mezuro_configuration) + MezuroConfiguration.any_instance.expects(:update).with(mezuro_configuration_params).returns(false) - post :update, :id => @subject.id, :mezuro_configuration => @subject_params + post :update, :id => mezuro_configuration.id, :mezuro_configuration => mezuro_configuration_params end it { is_expected.to render_template(:edit) } @@ -254,16 +248,16 @@ describe MezuroConfigurationsController, :type => :controller do context 'when the user does not own the mezuro_configuration' do before :each do - post :update, :id => @subject.id, :mezuro_configuration => @subject_params + post :update, :id => mezuro_configuration.id, :mezuro_configuration => mezuro_configuration_params end - it { is_expected.to redirect_to mezuro_configurations_path(@subject.id) } + it { is_expected.to redirect_to mezuro_configurations_path(mezuro_configuration.id) } end end context 'with no user logged in' do before :each do - post :update, :id => @subject.id, :mezuro_configuration => @subject_params + post :update, :id => mezuro_configuration.id, :mezuro_configuration => mezuro_configuration_params end it { is_expected.to redirect_to new_user_session_path } diff --git a/spec/models/mezuro_configuration_spec.rb b/spec/models/mezuro_configuration_spec.rb deleted file mode 100644 index 514e63d..0000000 --- a/spec/models/mezuro_configuration_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -require 'rails_helper' - -describe MezuroConfiguration, :type => :model do - subject { FactoryGirl.build(:mezuro_configuration) } - describe 'methods' do - describe 'persisted?' do - before :each do - MezuroConfiguration.expects(:exists?).with(subject.id).returns(false) - end - - it 'should return false' do - expect(subject.persisted?).to eq(false) - end - end - - describe 'update' do - before :each do - @subject_params = Hash[FactoryGirl.attributes_for(:mezuro_configuration).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 'with valid attributes' do - before :each do - subject.expects(:save).returns(true) - end - - it 'should return true' do - expect(subject.update(@subject_params)).to eq(true) - end - end - - context 'with invalid attributes' do - before :each do - subject.expects(:save).returns(false) - end - - it 'should return false' do - expect(subject.update(@subject_params)).to eq(false) - end - end - end - - describe 'metric_configurations' do - subject { FactoryGirl.build(:mezuro_configuration) } - let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } - - it 'should call metric_configurations_of on the Metric Configuration model' do - MetricConfiguration.expects(:metric_configurations_of).with(subject.id).returns([metric_configuration]) - - expect(subject.metric_configurations).to include(metric_configuration) - end - end - end - - describe 'validations' do - context 'active model validations' do - before :each do - MezuroConfiguration.expects(:all).at_least_once.returns([]) - end - it { is_expected.to validate_presence_of(:name) } - end - - context 'kalibro validations' do - before :each do - MezuroConfiguration.expects(:request).returns(42) - end - - it 'should validate uniqueness' do - KalibroUniquenessValidator.any_instance.expects(:validate_each).with(subject, :name, subject.name) - subject.save - end - end - end -end -- libgit2 0.21.2