From 03d3f74b5c1cd47624acaa93f9372524d5e70160 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Sat, 6 Oct 2018 23:31:06 +0200 Subject: [PATCH] Rewrite mocking and stubbing to only rspec Previously it was a mixed style. This unifies it to pure rspec style. --- .rubocop.yml | 3 +++ .sync.yml | 2 +- spec/spec_helper.rb | 4 ++++ spec/spec_helper_local.rb | 4 ---- spec/unit/mongodb_version_spec.rb | 4 ++-- .../provider/mongodb_database/mongodb_spec.rb | 11 ++++------ .../provider/mongodb_replset/mongodb_spec.rb | 11 +++++----- .../provider/mongodb_shard/mongodb_spec.rb | 8 ++++--- .../provider/mongodb_user/mongodb_spec.rb | 22 ++++++++++--------- 9 files changed, 37 insertions(+), 32 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 099a11c53..c52ac4283 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -543,3 +543,6 @@ Style/FormatStringToken: # are located. RSpec/FilePath: Enabled: false + +RSpec/MessageSpies: + EnforcedStyle: receive diff --git a/.sync.yml b/.sync.yml index aad14a6c2..6a5dea279 100644 --- a/.sync.yml +++ b/.sync.yml @@ -6,5 +6,5 @@ appveyor.yml: delete: true spec/spec_helper.rb: - allow_deprecations: true + mock_with: :rspec spec_overrides: "require 'spec_helper_local'" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f42593ecb..1d10aa754 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,10 @@ # This file is managed via modulesync # https://github.com/voxpupuli/modulesync # https://github.com/voxpupuli/modulesync_config +RSpec.configure do |c| + c.mock_with :rspec +end + require 'puppetlabs_spec_helper/module_spec_helper' require 'rspec-puppet-facts' include RspecPuppetFacts diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb index d286be97e..68de7ff17 100644 --- a/spec/spec_helper_local.rb +++ b/spec/spec_helper_local.rb @@ -1,7 +1,3 @@ -RSpec.configure do |config| - config.mock_with :rspec -end - def with_debian_facts let :facts do { diff --git a/spec/unit/mongodb_version_spec.rb b/spec/unit/mongodb_version_spec.rb index 0b136d6d0..063b4c664 100644 --- a/spec/unit/mongodb_version_spec.rb +++ b/spec/unit/mongodb_version_spec.rb @@ -8,8 +8,8 @@ describe 'mongodb_version' do context 'with value' do before do - Facter::Core::Execution.stubs(:which).with('mongo').returns(true) - Facter::Core::Execution.stubs(:execute).with('mongo --version 2>&1').returns('MongoDB shell version: 3.2.1') + allow(Facter::Core::Execution).to receive(:which).with('mongo').and_return(true) + allow(Facter::Core::Execution).to receive(:execute).with('mongo --version 2>&1').and_return('MongoDB shell version: 3.2.1') end it { expect(Facter.fact(:mongodb_version).value).to eq('3.2.1') diff --git a/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb b/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb index 4f39c0af9..0e313d853 100644 --- a/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb +++ b/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb @@ -20,8 +20,6 @@ }.to_json end - let(:parsed_dbs) { %w[admin local] } - let(:resource) do Puppet::Type.type(:mongodb_database).new( ensure: :present, @@ -38,27 +36,26 @@ tmp = Tempfile.new('test') mongodconffile = tmp.path allow(provider.class).to receive(:mongod_conf_file).and_return(mongodconffile) - provider.class.stubs(:mongo_eval).with('rs.slaveOk();printjson(db.getMongo().getDBs())').returns(raw_dbs) + allow(provider.class).to receive(:mongo_eval).with('rs.slaveOk();printjson(db.getMongo().getDBs())').and_return(raw_dbs) allow(provider.class).to receive(:db_ismaster).and_return(true) end describe 'self.instances' do it 'returns an array of dbs' do - dbs = provider.class.instances.map(&:name) - expect(parsed_dbs).to match_array(dbs) + expect(provider.class.instances.map(&:name)).to match_array(%w[admin local]) end end describe 'create' do it 'makes a database' do - provider.expects(:mongo_eval) + expect(provider).to receive(:mongo_eval) provider.create end end describe 'destroy' do it 'removes a database' do - provider.expects(:mongo_eval) + expect(provider).to receive(:mongo_eval) provider.destroy end end diff --git a/spec/unit/puppet/provider/mongodb_replset/mongodb_spec.rb b/spec/unit/puppet/provider/mongodb_replset/mongodb_spec.rb index badf3b879..248cff0a6 100644 --- a/spec/unit/puppet/provider/mongodb_replset/mongodb_spec.rb +++ b/spec/unit/puppet/provider/mongodb_replset/mongodb_spec.rb @@ -37,16 +37,17 @@ EOT end - # rubocop:disable RSpec/MessageSpies + # rubocop:disable RSpec/MultipleExpectations it 'creates a replicaset' do - allow(provider.class).to receive(:replset_properties) - allow(provider).to receive(:alive_members).and_return(valid_members) - allow(provider).to receive(:master_host).and_return(false) + expect(provider.class).to receive(:replset_properties) + expect(provider).to receive(:get_hosts_status).and_return([valid_members, []]) + expect(provider).to receive(:master_host).and_return(false) expect(provider).to receive(:rs_initiate).with('{"_id":"rs_test","members":[{"host":"mongo1:27017","_id":0},{"host":"mongo2:27017","_id":1},{"host":"mongo3:27017","_id":2}],"settings":{}}', 'mongo1:27017').and_return('info' => 'Config now saved locally. Should come online in about a minute.', 'ok' => 1) - allow(provider).to receive(:db_ismaster).and_return('{"ismaster" : true}') + expect(provider).to receive(:db_ismaster).and_return('{"ismaster" : true}') provider.create provider.flush end + # rubocop:enable RSpec/MultipleExpectations end describe '#exists?' do diff --git a/spec/unit/puppet/provider/mongodb_shard/mongodb_spec.rb b/spec/unit/puppet/provider/mongodb_shard/mongodb_spec.rb index af886f7f6..a6ad57f06 100644 --- a/spec/unit/puppet/provider/mongodb_shard/mongodb_spec.rb +++ b/spec/unit/puppet/provider/mongodb_shard/mongodb_spec.rb @@ -37,7 +37,7 @@ end before do - provider.class.stubs(:mongo_command).with('sh.status()').returns(raw_shards) + allow(provider.class).to receive(:mongo_command).with('sh.status()').and_return(raw_shards) end describe 'self.instances' do @@ -48,17 +48,19 @@ end describe '#create' do + # rubocop:disable RSpec/MultipleExpectations it 'makes a shard' do - provider.expects('sh_addshard').with('rs_test/mongo1:27018').returns( + expect(provider).to receive(:sh_addshard).with('rs_test/mongo1:27018').and_return( 'shardAdded' => 'rs_test', 'ok' => 1 ) - provider.expects('sh_enablesharding').with('rs_test').returns( + expect(provider).to receive(:sh_enablesharding).with('rs_test').and_return( 'ok' => 1 ) provider.create provider.flush end + # rubocop:enable RSpec/MultipleExpectations end describe 'destroy' do diff --git a/spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb b/spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb index d25804b91..24e99b5dd 100644 --- a/spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb +++ b/spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb @@ -30,8 +30,8 @@ tmp = Tempfile.new('test') mongodconffile = tmp.path allow(provider.class).to receive(:mongod_conf_file).and_return(mongodconffile) - provider.class.stubs(:mongo_eval).with('printjson(db.system.users.find().toArray())').returns(raw_users) - provider.class.stubs(:mongo_version).returns('2.6.x') + allow(provider.class).to receive(:mongo_eval).with('printjson(db.system.users.find().toArray())').and_return(raw_users) + allow(provider.class).to receive(:mongo_version).and_return('2.6.x') allow(provider.class).to receive(:db_ismaster).and_return(true) end @@ -61,14 +61,14 @@ } EOS - provider.expects(:mongo_eval).with("db.runCommand(#{cmd_json})", 'new_database') + expect(provider).to receive(:mongo_eval).with("db.runCommand(#{cmd_json})", 'new_database') provider.create end end describe 'destroy' do it 'removes a user' do - provider.expects(:mongo_eval).with("db.dropUser('new_user')") + expect(provider).to receive(:mongo_eval).with("db.dropUser('new_user')") provider.destroy end end @@ -94,7 +94,7 @@ "digestPassword": false } EOS - provider.expects(:mongo_eval). + expect(provider).to receive(:mongo_eval). with("db.runCommand(#{cmd_json})", 'new_database') provider.password_hash = 'newpass' end @@ -121,32 +121,34 @@ describe 'roles=' do it 'changes nothing' do resource.provider.set(name: 'new_user', ensure: :present, roles: %w[role1 role2]) - provider.expects(:mongo_eval).times(0) + expect(provider).not_to receive(:mongo_eval) provider.roles = %w[role1 role2] end it 'grant a role' do resource.provider.set(name: 'new_user', ensure: :present, roles: %w[role1 role2]) - provider.expects(:mongo_eval). + expect(provider).to receive(:mongo_eval). with("db.getSiblingDB('new_database').grantRolesToUser('new_user', [\"role3\"])") provider.roles = %w[role1 role2 role3] end it 'revokes a role' do resource.provider.set(name: 'new_user', ensure: :present, roles: %w[role1 role2]) - provider.expects(:mongo_eval). + expect(provider).to receive(:mongo_eval). with("db.getSiblingDB('new_database').revokeRolesFromUser('new_user', [\"role1\"])") provider.roles = ['role2'] end + # rubocop:disable RSpec/MultipleExpectations it 'exchanges a role' do resource.provider.set(name: 'new_user', ensure: :present, roles: %w[role1 role2]) - provider.expects(:mongo_eval). + expect(provider).to receive(:mongo_eval). with("db.getSiblingDB('new_database').revokeRolesFromUser('new_user', [\"role1\"])") - provider.expects(:mongo_eval). + expect(provider).to receive(:mongo_eval). with("db.getSiblingDB('new_database').grantRolesToUser('new_user', [\"role3\"])") provider.roles = %w[role2 role3] end + # rubocop:enable RSpec/MultipleExpectations end end