Class: RSpec::Rails::Matchers::Assign::AssignMatcher
- Inherits:
-
Object
- Object
- RSpec::Rails::Matchers::Assign::AssignMatcher
- Defined in:
- lib/rspec/rails/matchers/assign.rb
Overview
nodoc
Instance Attribute Summary collapse
-
#actual ⇒ Object
Returns the value of attribute actual.
-
#expected ⇒ Object
Returns the value of attribute expected.
-
#operator ⇒ Object
Returns the value of attribute operator.
Instance Method Summary collapse
- #description ⇒ Object
- #diffable? ⇒ Boolean
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
-
#initialize(scope, name, expected = nil) ⇒ AssignMatcher
constructor
A new instance of AssignMatcher.
- #matches?(actual) ⇒ Boolean
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
#actual ⇒ Object
Returns the value of attribute actual.
8 9 10 |
# File 'lib/rspec/rails/matchers/assign.rb', line 8 def actual @actual end |
#expected ⇒ Object
Returns the value of attribute expected.
8 9 10 |
# File 'lib/rspec/rails/matchers/assign.rb', line 8 def expected @expected end |
#operator ⇒ Object
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
#description ⇒ Object
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
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_should ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rspec/rails/matchers/assign.rb', line 37 def 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_not ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/rspec/rails/matchers/assign.rb', line 49 def 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
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 |