summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/matchers/have_instance_variable_spec.rb
blob: ababb38bc79ba7c755892fe590c472238a249ec4 (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
54
55
56
57
58
59
60
61
require 'spec_helper'
require 'mspec/expectations/expectations'
require 'mspec/matchers'

shared_examples_for "have_instance_variable, on all Ruby versions" do
  after :all do
    Object.const_set :RUBY_VERSION, @ruby_version
  end

  it "matches when object has the instance variable, given as string" do
    matcher = HaveInstanceVariableMatcher.new('@foo')
    matcher.matches?(@object).should be_true
  end

  it "matches when object has the instance variable, given as symbol" do
    matcher = HaveInstanceVariableMatcher.new(:@foo)
    matcher.matches?(@object).should be_true
  end

  it "does not match when object hasn't got the instance variable, given as string" do
    matcher = HaveInstanceVariableMatcher.new('@bar')
    matcher.matches?(@object).should be_false
  end

  it "does not match when object hasn't got the instance variable, given as symbol" do
    matcher = HaveInstanceVariableMatcher.new(:@bar)
    matcher.matches?(@object).should be_false
  end

  it "provides a failure message for #should" do
    matcher = HaveInstanceVariableMatcher.new(:@bar)
    matcher.matches?(@object)
    matcher.failure_message.should == [
      "Expected #{@object.inspect} to have instance variable '@bar'",
      "but it does not"
    ]
  end

  it "provides a failure messoge for #should_not" do
    matcher = HaveInstanceVariableMatcher.new(:@bar)
    matcher.matches?(@object)
    matcher.negative_failure_message.should == [
      "Expected #{@object.inspect} NOT to have instance variable '@bar'",
      "but it does"
    ]
  end
end

describe HaveInstanceVariableMatcher, "on RUBY_VERSION >= 1.9" do
  before :all do
    @ruby_version = Object.const_get :RUBY_VERSION
    Object.const_set :RUBY_VERSION, '1.9.0'

    @object = Object.new
    def @object.instance_variables
      [:@foo]
    end
  end

  it_should_behave_like "have_instance_variable, on all Ruby versions"
end