Commit 8dcf085c4d20f7039de15d6ea80d10f708dbd9e9
1 parent
b83ceca0
Exists in
colab
and in
4 other branches
KalibroConfiguration#destroy cleans attributes memoization
If the memoization was left it could lead to inconsistencies after destroying Signed off by: Heitor Reis <marcheing@gmail.com>
Showing
2 changed files
with
15 additions
and
4 deletions
Show diff stats
app/models/kalibro_configuration.rb
spec/models/kalibro_configuration_spec.rb
| ... | ... | @@ -77,15 +77,25 @@ describe KalibroConfiguration, :type => :model do |
| 77 | 77 | let!(:kalibro_configuration_attributes) { FactoryGirl.build(:kalibro_configuration_attributes) } |
| 78 | 78 | let!(:kalibro_configuration) { kalibro_configuration_attributes.kalibro_configuration } |
| 79 | 79 | |
| 80 | - before do | |
| 81 | - kalibro_configuration.expects(:attributes).twice.returns(kalibro_configuration_attributes) | |
| 82 | - end | |
| 83 | - | |
| 84 | 80 | it 'should be destroyed' do |
| 81 | + kalibro_configuration.expects(:attributes).twice.returns(kalibro_configuration_attributes) | |
| 85 | 82 | kalibro_configuration_attributes.expects(:destroy) |
| 86 | 83 | KalibroClient::Entities::Configurations::KalibroConfiguration.any_instance.expects(:destroy).returns(kalibro_configuration) |
| 87 | 84 | kalibro_configuration.destroy |
| 88 | 85 | end |
| 86 | + | |
| 87 | + it 'is expected to clean the attributes memoization' do | |
| 88 | + # Call attributes once so it memoizes | |
| 89 | + KalibroConfigurationAttributes.expects(:find_by).with(kalibro_configuration_id: kalibro_configuration.id).returns(kalibro_configuration_attributes) | |
| 90 | + expect(kalibro_configuration.attributes).to eq(kalibro_configuration_attributes) | |
| 91 | + | |
| 92 | + # Destroying | |
| 93 | + kalibro_configuration.destroy | |
| 94 | + | |
| 95 | + # The expectation call will try to find the attributes on the database which should be nil since it was destroyed | |
| 96 | + KalibroConfigurationAttributes.expects(:find_by).with(kalibro_configuration_id: kalibro_configuration.id).returns(nil) | |
| 97 | + expect(kalibro_configuration.attributes).to be_nil | |
| 98 | + end | |
| 89 | 99 | end |
| 90 | 100 | end |
| 91 | 101 | end | ... | ... |