Class: Spec::FactoryGirl::Matchers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/factory_girl/matchers/base.rb

Direct Known Subclasses

BeBuiltByFactory, BeCreatedByFactory

Instance Method Summary collapse

Constructor Details

#initialize(factory) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/spec/factory_girl/matchers/base.rb', line 8

def initialize(factory)
  @factory = factory
  @errors = []
end

Instance Method Details

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spec/factory_girl/matchers/base.rb', line 13

def matches?(target)
  prepare_matches(target)
  return false unless @result

  # record is a kind of target class
  unless @result.kind_of?(@target)
    @errors << "'#{@target}' class expected, but factory return object kind of #{@result.class}"
    return false
  end

  # record is valid
  unless @result.valid?
    @errors += @result.errors.full_messages
    return false
  end

  # record can be saved
  begin
    @result.save!
  rescue ActiveRecord::RecordInvalid => e
    @errors << e.to_s
    return false
  end

  # A second record can be created
  begin
    Factory.create(@factory)
  rescue ActiveRecord::RecordInvalid => e
    @errors << "model has uniqueness validation, use sequence in factory (#{e.to_s})"
    return false
  end

  @errors.empty?
end