diff options
author | Adam Edwards <adamed@opscode.com> | 2014-05-15 14:14:01 -0700 |
---|---|---|
committer | Adam Edwards <adamed@opscode.com> | 2014-05-15 14:14:01 -0700 |
commit | 1cf40fe00e65ac2e6911bce64154e5111687b90d (patch) | |
tree | 65b6391650c78d49a35b7bacd5bc5552fb971586 /spec | |
parent | d9de698a09a1ee4395bd06225e110e966d150e51 (diff) | |
download | wmi-lite-1cf40fe00e65ac2e6911bce64154e5111687b90d.tar.gz |
Delay connection to first query, not constructor
Diffstat (limited to 'spec')
-rw-r--r-- | spec/functional/wmi_spec.rb | 3 | ||||
-rw-r--r-- | spec/unit/wmi_spec.rb | 22 |
2 files changed, 24 insertions, 1 deletions
diff --git a/spec/functional/wmi_spec.rb b/spec/functional/wmi_spec.rb index badca78..53bf548 100644 --- a/spec/functional/wmi_spec.rb +++ b/spec/functional/wmi_spec.rb @@ -39,7 +39,8 @@ describe WmiLite::Wmi, :windows_only do shared_examples_for 'an invalid query' do it 'should raise an exception if an invalid namespace is specified' do - expect { invalid_wmi = WmiLite::Wmi.new('root/notvalid') }.to raise_error + invalid_wmi = WmiLite::Wmi.new('root/notvalid') + expect { invalid_wmi.ExecQuery('Win32_Process') }.to raise_error end it 'should raise an exception if an invalid class is specified' do diff --git a/spec/unit/wmi_spec.rb b/spec/unit/wmi_spec.rb index a6fb0b5..5aac6b9 100644 --- a/spec/unit/wmi_spec.rb +++ b/spec/unit/wmi_spec.rb @@ -199,6 +199,28 @@ describe WmiLite::Wmi do end + context "when constructing a Ruby class instance" do + it "should not connect to WMI in the constructor" do + WmiLite::Wmi.any_instance.should_not_receive(:connect_to_namespace) + wmi_service_nil_namespace = WmiLite::Wmi.new + wmi_service_explicit_namespace = WmiLite::Wmi.new('root/cimv2') + end + end + + context "when calling query methods" do + it "should only connect to WMI on the first query execution" do + WIN32OLE.should_receive(:new).with("WbemScripting.SWbemLocator").exactly(1).times.and_return(wbem_locator) + wmi_service = WmiLite::Wmi.new + + # Make a lot of queries to be sure the connection is only created once + wmi_service.query('select * from Win32_Process') + wmi_service.query('select * from Win32_Process') + wmi_service.instances_of('Win32_Processor') + wmi_service.instances_of('Win32_Processor') + wmi_service.first_of('Win32_Group') + wmi_service.first_of('Win32_Group') + end + end it_should_behave_like "the first_of method" |