vagrant-lxc-ng/spec/unit/support/unit_example_group.rb
Fabio Rehm 2e2c2fad56 Convert specs to RSpec 2.99.0.beta2 syntax with Transpec
This conversion is done by Transpec 1.10.2 with the following command:
    transpec

* 46 conversions
    from: obj.should
      to: expect(obj).to

* 20 conversions
    from: obj.stub(:message)
      to: allow(obj).to receive(:message)

* 10 conversions
    from: == expected
      to: eq(expected)

* 6 conversions
    from: obj.should_receive(:message)
      to: expect(obj).to receive(:message)

* 5 conversions
    from: obj.should_not
      to: expect(obj).not_to

* 5 conversions
    from: Klass.any_instance.stub(:message) { |arg| }
      to: Klass.any_instance.stub(:message) { |instance, arg| }

* 5 conversions
    from: Klass.any_instance.stub(:message)
      to: allow_any_instance_of(Klass).to receive(:message)

* 1 conversion
    from: lambda { }.should
      to: expect { }.to
2014-03-14 11:38:54 -03:00

38 lines
1.3 KiB
Ruby

module UnitExampleGroup
def self.included(base)
base.metadata[:type] = :unit
base.before do
allow_any_instance_of(Object).to receive(:system) { |instance, *args, &block|
UnitExampleGroup.prevent_system_calls(*args, &block)
}
allow_any_instance_of(Object).to receive(:`) { |instance, *args, &block|
UnitExampleGroup.prevent_system_calls(*args, &block)
}
allow_any_instance_of(Object).to receive(:exec) { |instance, *args, &block|
UnitExampleGroup.prevent_system_calls(*args, &block)
}
allow_any_instance_of(Object).to receive(:fork) { |instance, *args, &block|
UnitExampleGroup.prevent_system_calls(*args, &block)
}
allow_any_instance_of(Object).to receive(:spawn) { |instance, *args, &block|
UnitExampleGroup.prevent_system_calls(*args, &block)
}
require 'vagrant/util/subprocess'
allow(Vagrant::Util::Subprocess).to receive(:execute) { |*args, &block|
UnitExampleGroup.prevent_system_calls(*args, &block)
}
end
end
def self.prevent_system_calls(*args, &block)
args.pop if args.last.is_a?(Hash)
raise <<-MSG
Somehow your code under test is trying to execute a command on your system,
please stub it out or move your spec code to an acceptance spec.
Block: #{block.inspect}
Command: "#{args.join(' ')}"
MSG
end
end