summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/guards/conflict_spec.rb
blob: 7dbe83153d74384f9e7c5325d77f43a0370c2a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'spec_helper'
require 'mspec/guards'

RSpec.describe Object, "#conflicts_with" do
  before :each do
    hide_deprecation_warnings
    ScratchPad.clear
  end

  it "does not yield if Object.constants includes any of the arguments" do
    allow(Object).to receive(:constants).and_return(["SomeClass", "OtherClass"])
    conflicts_with(:SomeClass, :AClass, :BClass) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).not_to eq(:yield)
  end

  it "does not yield if Object.constants (as Symbols) includes any of the arguments" do
    allow(Object).to receive(:constants).and_return([:SomeClass, :OtherClass])
    conflicts_with(:SomeClass, :AClass, :BClass) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).not_to eq(:yield)
  end

  it "yields if Object.constants does not include any of the arguments" do
    allow(Object).to receive(:constants).and_return(["SomeClass", "OtherClass"])
    conflicts_with(:AClass, :BClass) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).to eq(:yield)
  end

  it "yields if Object.constants (as Symbols) does not include any of the arguments" do
    allow(Object).to receive(:constants).and_return([:SomeClass, :OtherClass])
    conflicts_with(:AClass, :BClass) { ScratchPad.record :yield }
    expect(ScratchPad.recorded).to eq(:yield)
  end
end

RSpec.describe Object, "#conflicts_with" do
  before :each do
    hide_deprecation_warnings
    @guard = ConflictsGuard.new
    allow(ConflictsGuard).to receive(:new).and_return(@guard)
  end

  it "sets the name of the guard to :conflicts_with" do
    conflicts_with(:AClass, :BClass) { }
    expect(@guard.name).to eq(:conflicts_with)
  end

  it "calls #unregister even when an exception is raised in the guard block" do
    expect(@guard).to receive(:unregister)
    expect do
      conflicts_with(:AClass, :BClass) { raise Exception }
    end.to raise_error(Exception)
  end
end