Commit e39025d4e340eb2a709f7c4a35204b0260bf3015

Authored by Diego Camarinha
Committed by Rafael Manzo
1 parent 7bc0465b

Unit tests for mezuro range creation.

signed-off-by: Renan Fichberg <rfichberg@gmail.com>
app/controllers/mezuro_ranges_controller.rb
1 1 include OwnershipAuthentication
2 2  
3 3 class MezuroRangesController < ApplicationController
4   -
5   - before_action :authenticate_user!, except: [:show, :index]
6   - before_action :metric_configuration_owner?, except: [:index]
  4 + before_action :authenticate_user!, except: [:show]
  5 + before_action :metric_configuration_owner?, only: [:new, :create]
7 6  
8 7 def new
9 8 @mezuro_range = MezuroRange.new
... ... @@ -26,9 +25,6 @@ class MezuroRangesController &lt; ApplicationController
26 25 def update
27 26 end
28 27  
29   - def index
30   - end
31   -
32 28 def edit
33 29 end
34 30  
... ...
spec/controllers/metric_configurations_controller_spec.rb
... ... @@ -56,7 +56,7 @@ describe MetricConfigurationsController do
56 56 sign_in FactoryGirl.create(:user)
57 57 end
58 58  
59   - context 'when the current user owns the reading group' do
  59 + context 'when the current user owns the metric configuration' do
60 60 before :each do
61 61 subject.expects(:mezuro_configuration_owner?).returns true
62 62 end
... ...
spec/controllers/mezuro_ranges_spec.rb
1 1 require 'spec_helper'
2 2  
3 3 describe MezuroRangesController do
4   -
5 4 describe 'new' do
6 5 let(:mezuro_range) { FactoryGirl.build(:mezuro_range) }
7 6 let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) }
... ... @@ -32,4 +31,43 @@ describe MezuroRangesController do
32 31 it { should respond_with(:redirect) }
33 32 end
34 33 end
35   -end
36 34 \ No newline at end of file
  35 +
  36 + describe 'create' do
  37 + let(:mezuro_range_params) { Hash[FactoryGirl.attributes_for(:mezuro_range).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
  38 + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) }
  39 + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) }
  40 + let(:mezuro_range) { FactoryGirl.build(:mezuro_range) }
  41 +
  42 + before do
  43 + sign_in FactoryGirl.create(:user)
  44 + end
  45 +
  46 + context 'when the current user owns the mezuro range' do
  47 + before :each do
  48 + subject.expects(:metric_configuration_owner?).returns true
  49 + end
  50 +
  51 + context 'with valid fields' do
  52 + before :each do
  53 + MezuroRange.any_instance.expects(:save).returns(true)
  54 +
  55 + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration_id: metric_configuration.id, mezuro_range: mezuro_range_params
  56 + end
  57 +
  58 + it { should respond_with(:redirect) }
  59 + end
  60 +
  61 + context 'with invalid fields' do
  62 + before :each do
  63 + MezuroRange.any_instance.expects(:save).returns(false)
  64 + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration)
  65 + Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([])
  66 +
  67 + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration_id: metric_configuration.id, mezuro_range: mezuro_range_params
  68 + end
  69 +
  70 + it { should render_template(:new) }
  71 + end
  72 + end
  73 + end
  74 +end
... ...