Class: RSpec::ProcessMocks::ChildProcessMessageExpectation

Inherits:
Mocks::MessageExpectation
  • Object
show all
Defined in:
lib/rspec/process_mocks/message_expectation.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ChildProcessMessageExpectation

Returns a new instance of ChildProcessMessageExpectation.



6
7
8
9
10
11
12
13
14
# File 'lib/rspec/process_mocks/message_expectation.rb', line 6

def initialize(*args)
  super

  # The temp files are used for interprocess communication.
  # This one stores the calls that are exact matches.
  @actual_tempfile = Tempfile.new("should-receive-#{@expected_from.object_id}-#{@sym}")
  # This one stores the similar calls.
  @similar_tempfile = Tempfile.new("should-receive-similar-#{@expected_from.object_id}-#{@sym}")
end

Instance Method Details

#actual_messagesObject

Reads back the actual/exact-match arguments written to the temp file. This is not a method in rspec.



44
45
46
47
# File 'lib/rspec/process_mocks/message_expectation.rb', line 44

def actual_messages
  @actual_tempfile.rewind
  @actual_tempfile.readlines
end

#actual_received_countObject

The following methods used to use instance variables. But we can’t do that because those aren’t shared between processes. These methods overwrite rspec’s behavior and instead keep the state in temp files.



52
53
54
# File 'lib/rspec/process_mocks/message_expectation.rb', line 52

def actual_received_count
  actual_messages.size
end

#advise(*args) ⇒ Object

This method is executed in the child process when the stubbed method is called with similar argument. Overwritten from rspec.



29
30
31
32
33
# File 'lib/rspec/process_mocks/message_expectation.rb', line 29

def advise(*args)
  @similar_tempfile.puts msg
  @similar_tempfile.flush
  similar_messages
end

#generate_errorObject



55
56
57
58
59
60
61
# File 'lib/rspec/process_mocks/message_expectation.rb', line 55

def generate_error
  if similar_messages.empty?
    @error_generator.raise_expectation_error(@sym, @expected_received_count, actual_received_count, *@args_expectation.args)
  else
    @error_generator.raise_similar_message_args_error(self, *similar_messages)
  end
end

#invoke(*args, &block) ⇒ Object

This method is executed in the child process when the stubbed method is called exact arguments Overwritten from rspec.



20
21
22
23
24
# File 'lib/rspec/process_mocks/message_expectation.rb', line 20

def invoke(*args, &block)
  msg = args.inspect
  @actual_tempfile.puts msg
  @actual_tempfile.flush
end

#matches_at_least_count?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rspec/process_mocks/message_expectation.rb', line 62

def matches_at_least_count?
  @at_least && actual_received_count >= @expected_received_count
end

#matches_at_most_count?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rspec/process_mocks/message_expectation.rb', line 65

def matches_at_most_count?
  @at_most && actual_received_count <= @expected_received_count
end

#matches_exact_count?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/rspec/process_mocks/message_expectation.rb', line 68

def matches_exact_count?
  @expected_received_count == actual_received_count
end

#similar_messagesObject

This overwrites the rspec implementation because we need to record similar messages in a temp file not instance variables.



37
38
39
40
# File 'lib/rspec/process_mocks/message_expectation.rb', line 37

def similar_messages
  @similar_tempfile.rewind
  @similar_tempfile.readlines
end