Module: SmartRspec::Support::Model::Assertions

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

Instance Method Summary collapse

Instance Method Details

#assert_association(type, associations) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/smart_rspec/support/model/assertions.rb', line 79

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



70
71
72
73
74
75
76
77
# File 'lib/smart_rspec/support/model/assertions.rb', line 70

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



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

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



96
97
98
99
100
101
102
103
104
# File 'lib/smart_rspec/support/model/assertions.rb', line 96

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

#validates_email_of(attr, validation) ⇒ Object



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

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

#validates_exclusion_of(attr, validation) ⇒ Object



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

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

#validates_format_of(attr, validation) ⇒ Object



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

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
      be_valid_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



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

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)
    be_valid_expectation(attr, value)
  end
end

#validates_length_of(attr, validation) ⇒ Object



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

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
      be_valid_expectation(attr, 'x' * n)
    end
  end
end

#validates_presence_of(attr, validation) ⇒ Object



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

def validates_presence_of(attr, validation)
  it 'is blank' do
    be_valid_expectation(attr, nil, subject.dup)
  end
end

#validates_uniqueness_of(attr, validation) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/smart_rspec/support/model/assertions.rb', line 58

def validates_uniqueness_of(attr, validation)
  it 'is already in use' do
    if !validation.is_a?(Hash) || !validation.has_key?(:mock)
      raise ArgumentError, 'A "mock" must be set when validating the uniqueness of a record'
    elsif subject.persisted? || subject.save
      mock, scope = validation.values_at(:mock, :scope)
      mock.send("#{scope}=", subject.send(scope)) unless scope.to_s.empty?
      be_valid_expectation(attr, subject.send(attr), mock)
    end
  end
end