Class: Spec::Expectations::Should::Change

Inherits:
Base show all
Defined in:
lib/spec/expectations/should/change.rb

Direct Known Subclasses

NotChange

Instance Method Summary collapse

Methods inherited from Base

#==, #=~, #default_message, #fail_with_message, #find_supported_sym

Constructor Details

#initialize(target, receiver = nil, message = nil, &block) ⇒ Change

Returns a new instance of Change.



6
7
8
9
10
11
12
13
# File 'lib/spec/expectations/should/change.rb', line 6

def initialize(target, receiver=nil, message=nil, &block)
  @block = block
  @target = target
  @receiver = receiver
  @message = message
  execute_change
  evaluate_change
end

Instance Method Details

#by(expected_delta) ⇒ Object



45
46
47
48
49
50
# File 'lib/spec/expectations/should/change.rb', line 45

def by(expected_delta)
  if actual_delta != expected_delta
    fail_with_message "#{message} should have been changed by #{expected_delta}, but was changed by #{actual_delta}"
  end
  self
end

#evaluate_changeObject



25
26
27
28
29
# File 'lib/spec/expectations/should/change.rb', line 25

def evaluate_change
  if @before_change == @after_change
    fail_with_message "#{message} should have changed, but is still #{@after_change.inspect}"
  end
end

#execute_changeObject



15
16
17
18
19
# File 'lib/spec/expectations/should/change.rb', line 15

def execute_change
  @before_change = @block.nil? ? @receiver.send(@message) : @block.call
  @target.call
  @after_change = @block.nil? ? @receiver.send(@message) : @block.call
end

#from(value) ⇒ Object



31
32
33
34
35
36
# File 'lib/spec/expectations/should/change.rb', line 31

def from(value)
  if @before_change != value
    fail_with_message "#{message} should have initially been #{value.inspect}, but was #{@before_change.inspect}"
  end
  self
end

#messageObject



21
22
23
# File 'lib/spec/expectations/should/change.rb', line 21

def message
  @message.nil? ? 'result' : @message
end

#to(value) ⇒ Object



38
39
40
41
42
43
# File 'lib/spec/expectations/should/change.rb', line 38

def to(value)
  if @after_change != value
    fail_with_message "#{message} should have been changed to #{value.inspect}, but is now #{@after_change.inspect}"
  end
  self
end