Class: Waylon::RSpec::TestSense

Inherits:
Sense
  • Object
show all
Defined in:
lib/waylon/rspec/test_sense.rb

Overview

Extras for RSpec to facilitate testing Waylon Skills

Class Method Summary collapse

Methods inherited from Sense

codify, config_namespace, group_class, mention, perform, queue

Methods included from BaseComponent

included

Class Method Details

.add_user_from_details(details) ⇒ Object



11
12
13
14
15
16
# File 'lib/waylon/rspec/test_sense.rb', line 11

def self.add_user_from_details(details)
  user_id = user_list.size
  user = user_class.new(user_id, details)
  user_list << user
  user
end

.channel_listArray<Integer>

The list of TestChannel IDs for this TestSense

Returns:

  • (Array<Integer>)


20
21
22
# File 'lib/waylon/rspec/test_sense.rb', line 20

def self.channel_list
  @channel_list ||= []
end

.enqueue(route, request) ⇒ Object

Overrides the Sense.enqueue class method to avoid Resque



25
26
27
28
29
30
31
32
33
# File 'lib/waylon/rspec/test_sense.rb', line 25

def self.enqueue(route, request)
  details = {
    "sense" => self,
    "request" => request,
    "route" => route.name
  }

  fake_queue.push [route.destination, details]
end

.fake_queueQueue

Allows access to the fake version of Resque

Returns:

  • (Queue)


37
38
39
# File 'lib/waylon/rspec/test_sense.rb', line 37

def self.fake_queue
  @fake_queue ||= Queue.new
end

.message_classClass

Ensures we’re using the TestMessage class for Messages

Returns:

  • (Class)


43
44
45
# File 'lib/waylon/rspec/test_sense.rb', line 43

def self.message_class
  RSpec::TestMessage
end

.message_from_request(request) ⇒ Waylon::Message

Provides a way to use an initial request to reconstitute a Sense-specific Message

Returns:



55
56
57
# File 'lib/waylon/rspec/test_sense.rb', line 55

def self.message_from_request(request)
  message_class.new(message_list.size - 1, request)
end

.message_listArray<Hash>

The list of message details that were sent through this Sense

Returns:

  • (Array<Hash>)


49
50
51
# File 'lib/waylon/rspec/test_sense.rb', line 49

def self.message_list
  @message_list ||= []
end

.react(request, type) ⇒ void

This method returns an undefined value.

Emulates reactions by sending a message with the reaction type

Parameters:

  • request (Integer)

    A reference (message ID) of the initial request

  • type (Symbol, String)

    The type of reaction to send



63
64
65
66
# File 'lib/waylon/rspec/test_sense.rb', line 63

def self.react(request, type)
  msg = message_from_request(request)
  msg.channel.post_message(":#{type}:")
end

.received_messagesArray<String>

Provides all message text sent to Waylon

Returns:

  • (Array<String>)


70
71
72
# File 'lib/waylon/rspec/test_sense.rb', line 70

def self.received_messages
  message_list.reject { |m| m[:user_id] == TestUser.whoami.id }.map { |m| m[:text] }
end

.reply(request, text) ⇒ void

This method returns an undefined value.

Posts a reply to the channel

Parameters:

  • request (Integer)

    A reference (message ID) of the initial request

  • text (String)

    The message content to send in response to the request



78
79
80
81
# File 'lib/waylon/rspec/test_sense.rb', line 78

def self.reply(request, text)
  msg = message_from_request(request)
  msg.channel.post_message(text)
end

.run(message_details) ⇒ void

This method returns an undefined value.

Receives incoming message details and places work on a queue to be performed by a Skill

Parameters:

  • message_details (Hash)

    The details necessary for creating a TestMessage



86
87
88
89
90
91
92
# File 'lib/waylon/rspec/test_sense.rb', line 86

def self.run(message_details)
  message_list << message_details
  message_id = message_list.size - 1
  msg = message_class.new(message_id)
  route = SkillRegistry.route(msg) || SkillRegistry.instance.default_route(msg)
  enqueue(route, message_details)
end

.sent_messagesArray<String>

Provides all message text sent by Waylon

Returns:

  • (Array<String>)


96
97
98
# File 'lib/waylon/rspec/test_sense.rb', line 96

def self.sent_messages
  message_list.select { |m| m[:user_id] == TestUser.whoami.id }.map { |m| m[:text] }
end

.user_classClass

Ensures we’re using the TestUser class for Users

Returns:

  • (Class)


102
103
104
# File 'lib/waylon/rspec/test_sense.rb', line 102

def self.user_class
  RSpec::TestUser
end

.user_listArray<User>

The list of Users for this TestSense

Returns:



108
109
110
# File 'lib/waylon/rspec/test_sense.rb', line 108

def self.user_list
  @user_list ||= []
end