Class: RSpec::ActiveRecord::CreateRecord

Inherits:
Matcher
  • Object
show all
Includes:
Matchers::BuiltIn::BaseMatcher::HashFormatting
Defined in:
lib/rspec/active_record/create_record.rb

Overview

A matcher

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ CreateRecord

Returns a new instance of CreateRecord.



9
10
11
12
# File 'lib/rspec/active_record/create_record.rb', line 9

def initialize(scope)
  @scope = scope
  super()
end

Instance Method Details

#descriptionObject



50
51
52
53
54
55
# File 'lib/rspec/active_record/create_record.rb', line 50

def description
  message = "create #{scope_name}"
  message += " #{@times} time#{"s" if @times != 1}" if @times
  message += " matching #{format_hash(@attributes)}" if @attributes
  improve_hash_formatting message
end

#failure_messageObject



57
58
59
60
61
62
63
64
# File 'lib/rspec/active_record/create_record.rb', line 57

def failure_message
  failure = "expected to #{description} but"
  if !match_times?
    "#{failure} created #{@new_records.size}"
  else
    add_diff "#{failure} did not"
  end
end

#failure_message_when_negatedObject



66
67
68
# File 'lib/rspec/active_record/create_record.rb', line 66

def failure_message_when_negated
  "expected not to #{description} but did"
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
# File 'lib/rspec/active_record/create_record.rb', line 41

def matches?(block)
  existing_ids = @scope.ids
  block.call

  @new_records = @scope.where.not(@scope.primary_key => existing_ids).to_a

  match_times? && match_attributes? && @new_records.present?
end

#matching(attributes) ⇒ Object

Make sure that created record matches attributes



15
16
17
18
# File 'lib/rspec/active_record/create_record.rb', line 15

def matching(attributes)
  @attributes = attributes
  self
end

#onceObject



25
26
27
# File 'lib/rspec/active_record/create_record.rb', line 25

def once
  times(1)
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rspec/active_record/create_record.rb', line 37

def supports_block_expectations?
  true
end

#thriceObject



33
34
35
# File 'lib/rspec/active_record/create_record.rb', line 33

def thrice
  times(3)
end

#times(times) ⇒ Object



20
21
22
23
# File 'lib/rspec/active_record/create_record.rb', line 20

def times(times)
  @times = times
  self
end

#twiceObject



29
30
31
# File 'lib/rspec/active_record/create_record.rb', line 29

def twice
  times(2)
end