Class: RSpec::Rails::Matchers::Assign::AssignMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rails/matchers/assign.rb

Overview

nodoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, name, expected = nil) ⇒ AssignMatcher

Returns a new instance of AssignMatcher.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec/rails/matchers/assign.rb', line 10

def initialize scope, name, expected=nil
  @scope = scope
  @actual = nil

  if name.is_a? Hash
    raise ArgumentError, "must test at least one assignment" unless name.size
    raise ArgumentError, "can only test one assign at a time" if name.size > 1

    @name, expected = name.first
  else
    @name = name
  end

  @expected = [expected]
  @operator = nil
end

Instance Attribute Details

#actualObject

Returns the value of attribute actual.



8
9
10
# File 'lib/rspec/rails/matchers/assign.rb', line 8

def actual
  @actual
end

#expectedObject

Returns the value of attribute expected.



8
9
10
# File 'lib/rspec/rails/matchers/assign.rb', line 8

def expected
  @expected
end

#operatorObject

Returns the value of attribute operator.



8
9
10
# File 'lib/rspec/rails/matchers/assign.rb', line 8

def operator
  @operator
end

Instance Method Details

#descriptionObject



27
28
29
30
31
32
33
34
35
# File 'lib/rspec/rails/matchers/assign.rb', line 27

def description
  if @expected.first.nil?
    "assign @#{@name}"
  elsif @expected.first.respond_to? :matches?
    "assign @#{@name} to #{@expected.first.description}"
  else
    "assign @#{@name} #{@operator} #{@expected.first.inspect}"
  end
end

#diffable?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rspec/rails/matchers/assign.rb', line 59

def diffable?
  not (@expected.first.is_a?(Regexp) or @expected.first.respond_to? :matches?)
end

#failure_message_for_shouldObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rspec/rails/matchers/assign.rb', line 37

def failure_message_for_should
  if @expected.first.nil?
    "expected to assign @#{@name}"
  elsif ['==','===', '=~'].include?(operator)
    "expected: #{expected.first.inspect}\n     got: #{actual.inspect} (using #{operator})"
  elsif @expected.first.respond_to? :matches?
    "expected: #{actual.inspect} to #{@expected.first.description}"
  else
    "expected: #{operator} #{expected.first.inspect}\n     got: #{operator.gsub(/./, ' ')} #{actual.inspect}"
  end
end

#failure_message_for_should_notObject



49
50
51
52
53
54
55
56
57
# File 'lib/rspec/rails/matchers/assign.rb', line 49

def failure_message_for_should_not
  if @expected.first.nil?
    "expected not to assign @#{@name}"
  elsif @expected.first.respond_to? :matches?
    "expected: #{actual.inspect} to not #{@expected.first.description}"
  else
    "expected not: #{operator} #{expected.first.inspect}\n         got: #{operator.gsub(/./, ' ')} #{actual.inspect}"
  end
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rspec/rails/matchers/assign.rb', line 63

def matches? actual
  @actual = actual.assigns[@name]

  if @expected.first.nil?
    @actual.present?
  elsif @operator
    @actual.send @operator, @expected.first
  elsif @expected.first.is_a? Regexp
    @operator = "=~"
    @actual =~ @expected.first
  elsif @expected.first.respond_to? :matches?
    @expected.first.matches? @actual
  else
    @operator = "=="
    @actual == @expected.first
  end
end