summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/matchers/have_class_variable_spec.rb
blob: d6fcf9d4e2cb2527d52f4e916bd8650459cbb96c (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
require 'spec_helper'
require 'mspec/expectations/expectations'
require 'mspec/matchers'

class IVarModMock
  def self.class_variables
    [:@foo]
  end
end

RSpec.describe HaveClassVariableMatcher, "on RUBY_VERSION >= 1.9" do
  it "matches when mod has the class variable, given as string" do
    matcher = HaveClassVariableMatcher.new('@foo')
    expect(matcher.matches?(IVarModMock)).to be_truthy
  end

  it "matches when mod has the class variable, given as symbol" do
    matcher = HaveClassVariableMatcher.new(:@foo)
    expect(matcher.matches?(IVarModMock)).to be_truthy
  end

  it "does not match when mod hasn't got the class variable, given as string" do
    matcher = HaveClassVariableMatcher.new('@bar')
    expect(matcher.matches?(IVarModMock)).to be_falsey
  end

  it "does not match when mod hasn't got the class variable, given as symbol" do
    matcher = HaveClassVariableMatcher.new(:@bar)
    expect(matcher.matches?(IVarModMock)).to be_falsey
  end

  it "provides a failure message for #should" do
    matcher = HaveClassVariableMatcher.new(:@bar)
    matcher.matches?(IVarModMock)
    expect(matcher.failure_message).to eq([
      "Expected IVarModMock to have class variable '@bar'",
      "but it does not"
    ])
  end

  it "provides a failure messoge for #should_not" do
    matcher = HaveClassVariableMatcher.new(:@bar)
    matcher.matches?(IVarModMock)
    expect(matcher.negative_failure_message).to eq([
      "Expected IVarModMock NOT to have class variable '@bar'",
      "but it does"
    ])
  end
end