Module: SmartRspec::Support::Assertions

Included in:
Macros
Defined in:
lib/smart_rspec/support/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_association(type, associations) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/smart_rspec/support/assertions.rb', line 86

def assert_association(type, associations)
  associations.each do |model|
    it "#{type.to_s.gsub('_', ' ')} #{model}" do
      expect(subject).to respond_to(model)
      association_expectation(type, model)
    end
  end
end

#assert_has_attributes(attrs, options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/smart_rspec/support/assertions.rb', line 75

def assert_has_attributes(attrs, options)
  type_str = build_type_str(options)

  attrs.each do |attr|
    it %Q(has an attribute named "#{attr}"#{type_str}) do
      expect(subject).to respond_to(attr)
      has_attributes_expectation(attr, options)
    end
  end
end

#build_length_validation(key, value) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/smart_rspec/support/assertions.rb', line 95

def build_length_validation(key, value)
  case key
  when :in, :within then ['is out of the length range', value.max + 1]
  when :is, :minimum then ["is #{key == :is ? 'invalid' : 'too short'}", value - 1]
  when :maximum then ['is too long', value + 1]
  end
end

#build_type_str(options) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/smart_rspec/support/assertions.rb', line 103

def build_type_str(options)
  if !options.nil? && options[:type]
    " (%s%s%s)" % [
      ('Enumerated ' if options[:enum]),
      options[:type],
      (", default: #{options[:default]}" if options[:default])
    ]
  end
end

#scoped_validation?(validation) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/smart_rspec/support/assertions.rb', line 113

def scoped_validation?(validation)
  validation.is_a?(Hash) && ([:scope, :mock] - validation.keys).empty?
end

#validates_email_of(attr, validation) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/smart_rspec/support/assertions.rb', line 6

def validates_email_of(attr, validation)
  it 'has an invalid format' do
    %w(foobar foobar@ @foobar foo@bar).each do |e|
      validation_expectation(attr, e)
    end
  end
end

#validates_exclusion_of(attr, validation) ⇒ Object



14
15
16
17
18
# File 'lib/smart_rspec/support/assertions.rb', line 14

def validates_exclusion_of(attr, validation)
  it 'has a reserved value' do
    validation_expectation(attr, validation[:in].sample)
  end
end

#validates_format_of(attr, validation) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/smart_rspec/support/assertions.rb', line 20

def validates_format_of(attr, validation)
  it 'does not match the required format' do
    mock, with =
      validation.values_at(:mock).first,
      validation.values_at(:with).first

    if mock && with && with !~ mock
      validation_expectation(attr, mock)
    else
      raise ArgumentError, ':with and :mock are required when using the :format validation'
    end
  end
end

#validates_inclusion_of(attr, validation) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/smart_rspec/support/assertions.rb', line 34

def validates_inclusion_of(attr, validation)
  it 'is out of the scope of possible values' do
    begin
      value = SecureRandom.hex
    end while validation[:in].include?(value)
    validation_expectation(attr, value)
  end
end

#validates_length_of(attr, validation) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/smart_rspec/support/assertions.rb', line 43

def validates_length_of(attr, validation)
  validation.each do |key, value|
    next unless [:in, :is, :maximum, :minimum, :within].include?(key)
    txt, n = build_length_validation(key, value)
    it txt do
      validation_expectation(attr, 'x' * n)
    end
  end
end

#validates_presence_of(attr, validation) ⇒ Object



53
54
55
56
57
# File 'lib/smart_rspec/support/assertions.rb', line 53

def validates_presence_of(attr, validation)
  it 'is blank' do
    validation_expectation(attr, nil)
  end
end

#validates_uniqueness_of(attr, validation) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/smart_rspec/support/assertions.rb', line 59

def validates_uniqueness_of(attr, validation)
  scoped = scoped_validation?(validation)
  it "is already in use#{" (scope: #{validation[:scope]})" if scoped}" do
    mock =
      if scoped
        copy = subject.send(validation[:scope])
        validation[:mock].send("#{validation[:scope]}=", copy)
        validation[:mock]
      else
        subject.dup
      end
    subject.save unless subject.persisted?
    validation_expectation(attr, subject.send(attr), mock)
  end
end