Skip to main content

MessagingServiceMock

This item only works when running on the server. Server

In-memory stand-in for Roblox's MessagingService used by tests. It provides in-process loopback: a MessagingServiceMock.PublishAsync on a topic delivers the message to every current subscriber of that topic on the next task step, shaped exactly like the real MessagingService delivers it ({ Data = message, Sent = <number> }).

Messages are round-tripped through JSON on delivery (mimicking the real serialization), so aliasing bugs surface the same way they would against the real service.

It is injected into a PlaceMessagingService via PlaceMessagingService.SetRobloxMessagingService so tests never touch the real MessagingService.

local messagingService = MessagingServiceMock.new()
placeMessagingService:SetRobloxMessagingService(messagingService)

Functions

isMessagingServiceMock

MessagingServiceMock.isMessagingServiceMock(valueany) → boolean

Returns whether the given value is a MessagingServiceMock.

new

MessagingServiceMock.new() → MessagingServiceMock

Constructs a new MessagingServiceMock.

PublishAsync

MessagingServiceMock.PublishAsync(
topicstring,
messageany?
) → ()

Mimics MessagingService:PublishAsync. Delivers the message to every current subscriber of the topic on the next task step, shaped as { Data = message, Sent = <number> }. The message is round-tripped through JSON, mimicking the real service's serialization.

SubscribeAsync

MessagingServiceMock.SubscribeAsync(
topicstring,
callback(SubscriptionData) → ()
) → {
Connectedboolean,
Disconnect() → (),
Destroy() → ()
}

Mimics MessagingService:SubscribeAsync. Returns a connection-like object that removes the subscriber when disconnected (or destroyed by a [Maid]).

Show raw api
{
    "functions": [
        {
            "name": "isMessagingServiceMock",
            "desc": "Returns whether the given value is a [MessagingServiceMock].",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 57,
                "path": "src/messagingserviceutils/src/Server/Mocks/MessagingServiceMock.lua"
            }
        },
        {
            "name": "new",
            "desc": "Constructs a new MessagingServiceMock.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "MessagingServiceMock"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 66,
                "path": "src/messagingserviceutils/src/Server/Mocks/MessagingServiceMock.lua"
            }
        },
        {
            "name": "PublishAsync",
            "desc": "Mimics `MessagingService:PublishAsync`. Delivers the message to every current subscriber of the\ntopic on the next task step, shaped as `{ Data = message, Sent = <number> }`. The message is\nround-tripped through JSON, mimicking the real service's serialization.",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MessagingServiceMock"
                },
                {
                    "name": "topic",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "message",
                    "desc": "",
                    "lua_type": "any?"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 83,
                "path": "src/messagingserviceutils/src/Server/Mocks/MessagingServiceMock.lua"
            }
        },
        {
            "name": "SubscribeAsync",
            "desc": "Mimics `MessagingService:SubscribeAsync`. Returns a connection-like object that removes the\nsubscriber when disconnected (or destroyed by a [Maid]).",
            "params": [
                {
                    "name": "self",
                    "desc": "",
                    "lua_type": "MessagingServiceMock"
                },
                {
                    "name": "topic",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "(SubscriptionData) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ Connected: boolean, Disconnect: () -> (), Destroy: () -> () }"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 116,
                "path": "src/messagingserviceutils/src/Server/Mocks/MessagingServiceMock.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "MessagingServiceMock",
    "desc": "In-memory stand-in for Roblox's `MessagingService` used by tests. It provides in-process\nloopback: a [MessagingServiceMock.PublishAsync] on a topic delivers the message to every\ncurrent subscriber of that topic on the next task step, shaped exactly like the real\n`MessagingService` delivers it (`{ Data = message, Sent = <number> }`).\n\nMessages are round-tripped through JSON on delivery (mimicking the real serialization), so\naliasing bugs surface the same way they would against the real service.\n\nIt is injected into a [PlaceMessagingService] via\n[PlaceMessagingService.SetRobloxMessagingService] so tests never touch the real\n`MessagingService`.\n\n```lua\nlocal messagingService = MessagingServiceMock.new()\nplaceMessagingService:SetRobloxMessagingService(messagingService)\n```",
    "realm": [
        "Server"
    ],
    "source": {
        "line": 23,
        "path": "src/messagingserviceutils/src/Server/Mocks/MessagingServiceMock.lua"
    }
}