Commit d52765efa82013cd44cea3d217a331e086df488b
1 parent
0446d5b6
Exists in
colab
and in
4 other branches
Added unit tests for the update method
signed-off-by: Pedro Paulo Vezza Campos <pedro@vezza.com.br>
Showing
1 changed file
with
51 additions
and
0 deletions
Show diff stats
spec/controllers/compound_metric_configurations_controller_spec.rb
| ... | ... | @@ -121,4 +121,55 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 121 | 121 | it { is_expected.to redirect_to new_user_session_path } |
| 122 | 122 | end |
| 123 | 123 | end |
| 124 | + | |
| 125 | + describe 'update' do | |
| 126 | + let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration) } | |
| 127 | + let(:metric_configuration_params) { Hash[FactoryGirl.attributes_for(:compound_metric_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 | |
| 128 | + | |
| 129 | + | |
| 130 | + context 'when the user is logged in' do | |
| 131 | + before do | |
| 132 | + sign_in FactoryGirl.create(:user) | |
| 133 | + end | |
| 134 | + | |
| 135 | + context 'when user owns the metric configuration' do | |
| 136 | + before :each do | |
| 137 | + subject.expects(:metric_configuration_owner?).returns true | |
| 138 | + end | |
| 139 | + | |
| 140 | + context 'with valid fields' do | |
| 141 | + before :each do | |
| 142 | + MetricConfiguration.expects(:find).at_least_once.with(compound_metric_configuration.id).returns(compound_metric_configuration) | |
| 143 | + MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true) | |
| 144 | + | |
| 145 | + post :update, mezuro_configuration_id: compound_metric_configuration.configuration_id, id: compound_metric_configuration.id, metric_configuration: metric_configuration_params | |
| 146 | + end | |
| 147 | + | |
| 148 | + it { should redirect_to(mezuro_configuration_path(compound_metric_configuration.configuration_id)) } | |
| 149 | + it { should respond_with(:redirect) } | |
| 150 | + end | |
| 151 | + | |
| 152 | + context 'with an invalid field' do | |
| 153 | + before :each do | |
| 154 | + MetricConfiguration.expects(:find).at_least_once.with(compound_metric_configuration.id).returns(compound_metric_configuration) | |
| 155 | + MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false) | |
| 156 | + | |
| 157 | + post :update, mezuro_configuration_id: compound_metric_configuration.configuration_id, id: compound_metric_configuration.id, metric_configuration: metric_configuration_params | |
| 158 | + end | |
| 159 | + | |
| 160 | + it { should render_template(:edit) } | |
| 161 | + end | |
| 162 | + | |
| 163 | + end | |
| 164 | + | |
| 165 | + context 'when the user does not own the reading' do | |
| 166 | + before :each do | |
| 167 | + post :update, mezuro_configuration_id: compound_metric_configuration.configuration_id, id: compound_metric_configuration.id, metric_configuration: metric_configuration_params | |
| 168 | + end | |
| 169 | + | |
| 170 | + it { should redirect_to mezuro_configurations_path(compound_metric_configuration.configuration_id) } | |
| 171 | + end | |
| 172 | + end | |
| 173 | + end | |
| 174 | + | |
| 124 | 175 | end | ... | ... |