Test Client Overrides
Test Harness allows test authors to override various clients (AppClient, BotClient, UserClient) when running tests. Test Harness also allows test authors to provide implementations for the Slack API methods without a default implementation by the test harness. Negative testing of Slack APIs is allowed as well.
To mock the clients, the test author must extend from the proper client mock (AppClientMock, BotClientMock, UserClientMock). The tester then provides override implementations for the API methods to mock. The implementation must receive the request payload as the parameter and return a response payload as a result. You can override multiple methods in the same mock, but any methods that aren’t overridden default to the implementation that the test harness provides.
Methods with a default implementation:
- chatPostEphemeral
- chatPostMessage
- conversationsInfo
- usersInfo
- viewsOpen
- viewsPublish
- viewsPush
- viewsUpdate
If you provide a constructor implementation for your mock client, you must call the super method to insure access to the default api method implementations.
Note
To use the mock implementation it must be set on the Slack State object by calling the proper method in the test.
- SlackState.setAppClientMock(AppClientMock)
- SlackState.setBotClientMock(BotClientMock)
- SlackState.setUserClientMock(UserClientMock)
It’s optional, but recommended, to clear the mock clients after the test finishes by calling the proper methods in the test.
Alternatively you can call SlackState.clearAllClientMocks() to clear them all at the same time.
Apex Example
1@isTest
2public class ExampleMockOverrideTest {
3
4 private static Slack.TestHarness testHarness;
5 private static Slack.TestHarness.State slackState;
6
7 static {
8 testHarness = new Slack.TestHarness();
9 slackState = testHarness.getNewSlackState();
10 }
11
12 @isTest
13 static void testReactionsGet() {
14 slackState.setBotClientMock(new MyBotClientMock());
15 // run commands, shortcuts, events, etc
16 slackState.clearBotClientMock();
17 }
18
19 class MyBotClientMock extends Slack.BotClientMock {
20 public override Slack.ReactionsGetResponse reactionsGet(Slack.ReactionsGetRequest request) {
21 Slack.ReactionsGetResponse response = new Slack.ReactionsGetResponse();
22 response.setOk(true);
23 Slack.ReactionsGetResponse.Message message = new Slack.ReactionsGetResponse.Message();
24 Slack.Reaction reaction = new Slack.Reaction();
25 reaction.setName('party');
26 reaction.setCount(3);
27 List<Slack.Reaction> reactions = new List<Slack.Reaction>();
28 reactions.add(reaction);
29 message.setReactions(reactions);
30 message.setText('Test Reactions Message');
31 response.setMessage(message);
32 return response;
33 }
34 }
35
36}Beta Feature