spec/mongoid/clients_spec.rb in mongoid-5.1.4 vs spec/mongoid/clients_spec.rb in mongoid-5.1.5

- old
+ new

@@ -729,30 +729,45 @@ Mongoid::Config.load!(file, :test) end context "when the override is global" do - before do - Mongoid.override_database(:mongoid_optional) - end + shared_examples_for "a global database override" do - after do - Band.delete_all - Mongoid.override_database(nil) - end + before do + Mongoid.override_database(:mongoid_optional) + end - let!(:band) do - Band.create(name: "Tool") - end + after do + Band.delete_all + Mongoid.override_database(nil) + end - it "persists to the overridden database" do - Band.mongo_client.with(database: :mongoid_optional) do |sess| - expect(sess[:bands].find(name: "Tool")).to_not be_nil + let!(:band) do + klass.create(name: "Tool") end + + it "persists to the overridden database" do + mongo_client = Band.mongo_client.with(database: :mongoid_optional) + expect(mongo_client[:bands].count(name: "Tool")).to eq(1) + end + + it 'uses that database for the model mongo_client' do + expect(Band.mongo_client.database.name).to eq('mongoid_optional') + end end - it 'uses that database for the model mongo_client' do - expect(Band.mongo_client.database.name).to eq('mongoid_optional') + context "when normal usage" do + let(:klass) { Band } + + it_behaves_like "a global database override" + end + + context "when overriding the persistence options" do + + let(:klass) { Band.with(connect_timeout: 10) } + + it_behaves_like "a global database override" end end end end