Proofreading
This commit is contained in:
parent
c67d68dcf7
commit
7689a7bcca
@ -18,7 +18,7 @@ public void sayHello() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This is how you would define a method that says hello in the basic API. How do you go around testing it? If you do attempt to Junit test this method, what will you be testing? If change the method signature to return the string sent, then you can test the hello message content. However, can you test that you've actually `executed` the command?
|
This is how you would define a method that says hello in the basic API. How do you go around testing it? If you do attempt to Junit test this method, what will you be testing? If you change the method signature to return the string sent, then you can test the hello message content. However, can you test that you've actually `executed` the command?
|
||||||
|
|
||||||
## Mock Testing
|
## Mock Testing
|
||||||
*This section assumes you're familiar with mock testing. Mock testing is basically replacing a real object X with a fake object Y (a mock) of the same type. By doing that, you're able to test whether certain functions were executed.*
|
*This section assumes you're familiar with mock testing. Mock testing is basically replacing a real object X with a fake object Y (a mock) of the same type. By doing that, you're able to test whether certain functions were executed.*
|
||||||
@ -28,9 +28,11 @@ Obviously, you can't, but there's a twist to it. You can always mock the whole b
|
|||||||
## MessageSender Interface
|
## MessageSender Interface
|
||||||
All ability bots declare two utility objects.
|
All ability bots declare two utility objects.
|
||||||
### The Sender Object
|
### The Sender Object
|
||||||
The sender object is an implementation of the [MessageSender](../../telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java) interface. The interface mirrors
|
The `sender` object is an implementation of the [MessageSender](../../telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java) interface. The interface mirrors
|
||||||
all the bot sending methods. A user can supply his own MessageSender, but the AbilityBot module specifies a [DefaultSender](../../telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java) As you might guess, the default sender is simply a proxy for the bot API methods.
|
all the bot sending methods. A user can supply his own MessageSender, but the AbilityBot module specifies a [DefaultSender](../../telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java) As you might guess, the default sender is simply a proxy for the bot API methods.
|
||||||
|
|
||||||
|
### The Silent Object
|
||||||
|
The `silent` object is exactly like the sender object, but silent. Its methods return `Optional<T>`. On exception, it will be an empty optional. The sender object is provided to reduce verboseness of the code (reducing try-catch blocks with something more elegant).
|
||||||
## AbilityBot Testing
|
## AbilityBot Testing
|
||||||
Let's suppose that you have an ability that says "Hello World!" declared as such:
|
Let's suppose that you have an ability that says "Hello World!" declared as such:
|
||||||
```java
|
```java
|
||||||
@ -169,4 +171,23 @@ public class ExampleBotTest {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Silent Testing
|
## Silent Testing
|
||||||
As mentioned before, we also have another object that is able to send messages silently called `silent`. If you're using this object, you will also need to mock it and use it in the test. It is handled in exactly the same way.
|
As mentioned before, we also have another object that is able to send messages silently called `silent`. The constructor of the silent sender requires a MessageSender object. If your abilities use the `silent` object, be sure to:
|
||||||
|
```java
|
||||||
|
public class ExampleBotTest {
|
||||||
|
...
|
||||||
|
private DBContext db;
|
||||||
|
private MessageSender sender;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
bot = new ExampleBot(db);
|
||||||
|
sender = mock(MessageSender.class);
|
||||||
|
bot.silent = new SilentSender(sender);
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Do note that in your test assertions, don't use the silent object. Mocked assertion require the mock object. If you recall, the silent object uses the sender object, so your tests will still be correct if you're asserting on the `sender` object rather than the silent one.
|
@ -3,7 +3,7 @@ AbilityBots come with an embedded DB. Users are free to implement their own data
|
|||||||
The abstraction has multiple constructors to accommodate user-defined implementations of [DBContext](../../telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/DBContext.java) and [MessageSender](../../telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java). We'll talk about the message sender interface in the [[Bot Testing|Bot-Testing]] section.
|
The abstraction has multiple constructors to accommodate user-defined implementations of [DBContext](../../telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/db/DBContext.java) and [MessageSender](../../telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java). We'll talk about the message sender interface in the [[Bot Testing|Bot-Testing]] section.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
We'll be introducing an ability that maintains a special counter for every user. At every /increment, the user will receive a message with the previous number + 1. We'll initially start from zero and increment onwards.
|
We'll be introducing an ability that maintains a special counter for every user. At every /increment, the user will receive a message with the previous number + 1. We'll initially start from zero and increment upwards.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/**
|
/**
|
||||||
|
@ -73,5 +73,5 @@ public Ability playWithMe() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
In this example, we showcase how we can supply our own predicates. The two new predicates are `isReplyToMessage` and `isReplyToBot`.
|
In this example, we showcase how we can supply our own predicates. The two new predicates are `isReplyToMessage` and `isReplyToBot`.
|
||||||
The checks are made so that, once you execute your logic there is not need to check for the validity of the reply.
|
The checks are made so that, once you execute your logic there is no need to check for the validity of the reply.
|
||||||
They were all made once the action logic is being executed.
|
They were all made once the action logic is being executed.
|
Loading…
Reference in New Issue
Block a user