Class: RSpec::Matchers::Have
- Inherits:
-
Object
- Object
- RSpec::Matchers::Have
show all
- Defined in:
- lib/rspec/matchers/have.rb
Overview
Instance Method Summary
collapse
Constructor Details
#initialize(expected, relativity = :exactly) ⇒ Have
Returns a new instance of Have.
4
5
6
7
8
|
# File 'lib/rspec/matchers/have.rb', line 4
def initialize(expected, relativity=:exactly)
@expected = (expected == :no ? 0 : expected)
@relativity = relativity
@actual = @collection_name = @plural_collection_name = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
76
77
78
79
80
81
82
83
84
|
# File 'lib/rspec/matchers/have.rb', line 76
def method_missing(method, *args, &block)
@collection_name = method
if inflector = (defined?(ActiveSupport::Inflector) && ActiveSupport::Inflector.respond_to?(:pluralize) ? ActiveSupport::Inflector : (defined?(Inflector) ? Inflector : nil))
@plural_collection_name = inflector.pluralize(method.to_s)
end
@args = args
@block = block
self
end
|
Instance Method Details
#description ⇒ Object
66
67
68
|
# File 'lib/rspec/matchers/have.rb', line 66
def description
"have #{relative_expectation} #{@collection_name}"
end
|
#failure_message_for_should ⇒ Object
40
41
42
|
# File 'lib/rspec/matchers/have.rb', line 40
def failure_message_for_should
"expected #{relative_expectation} #{@collection_name}, got #{@actual}"
end
|
#failure_message_for_should_not ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/rspec/matchers/have.rb', line 44
def failure_message_for_should_not
if @relativity == :exactly
return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}"
elsif @relativity == :at_most
return "Isn't life confusing enough?\nInstead of having to figure out the meaning of this:\n should_not have_at_most(\#{@expected}).\#{@collection_name}\nWe recommend that you use this instead:\n should have_at_least(\#{@expected + 1}).\#{@collection_name}\n"
elsif @relativity == :at_least
return "Isn't life confusing enough?\nInstead of having to figure out the meaning of this:\n should_not have_at_least(\#{@expected}).\#{@collection_name}\nWe recommend that you use this instead:\n should have_at_most(\#{@expected - 1}).\#{@collection_name}\n"
end
end
|
#matches?(collection_owner) ⇒ Boolean
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/rspec/matchers/have.rb', line 18
def matches?(collection_owner)
if collection_owner.respond_to?(@collection_name)
collection = collection_owner.__send__(@collection_name, *@args, &@block)
elsif (@plural_collection_name && collection_owner.respond_to?(@plural_collection_name))
collection = collection_owner.__send__(@plural_collection_name, *@args, &@block)
elsif (collection_owner.respond_to?(:length) || collection_owner.respond_to?(:size))
collection = collection_owner
else
collection_owner.__send__(@collection_name, *@args, &@block)
end
@actual = collection.size if collection.respond_to?(:size)
@actual = collection.length if collection.respond_to?(:length)
raise not_a_collection if @actual.nil?
return @actual >= @expected if @relativity == :at_least
return @actual <= @expected if @relativity == :at_most
return @actual == @expected
end
|
#not_a_collection ⇒ Object
36
37
38
|
# File 'lib/rspec/matchers/have.rb', line 36
def not_a_collection
"expected #{@collection_name} to be a collection but it does not respond to #length or #size"
end
|
#relativities ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/rspec/matchers/have.rb', line 10
def relativities
@relativities ||= {
:exactly => "",
:at_least => "at least ",
:at_most => "at most "
}
end
|
#respond_to?(sym) ⇒ Boolean
70
71
72
|
# File 'lib/rspec/matchers/have.rb', line 70
def respond_to?(sym)
@expected.respond_to?(sym) || super
end
|