Closed chs97 closed 4 years ago
@huan please publish the last commit, wechaty-puppet-mock, I see the mocker initial twice.
It's great to see that you are trying to use the mocker to unit testing your bot!
How to receive the message from the bot with our mocker system?
It's easy!
test('reply', async (t) => {
const mocker = new Mocker()
const puppet = new PuppetMock({ mocker })
const wechaty = new Wechaty({ puppet })
await wechaty.start()
const [user, contact] = mocker.createContacts(3)
await mocker.login(user)
// ATTENTION: you need to register your listener before you say anything
wechaty.on('message', async (message) => {
await message.say('world')
})
// how to write test case ? I want to get the reply message.
// SOLUTION:
let reply
contact.on('message', message => {
reply = message.text()
})
// contact say something to user
contact.say('Hello').to(user)
await wechaty.stop()
// FINALLY!
assert(reply === 'world')
})
I hope this example can explain how to use the mocker system for you.
This weekend, I'm working on another complex unit test with the mocker and a text game: Math Master, which is one of our Wechaty Vorpal Extensions.
The Math Master game is an interactive text game between the bot and users, the rule is very sample: the bot will ask math questions, and the user needs to answer it in a period of time. In order to test this game, we need the power of the mocker system, with more testing codes.
You can expect to read the unit tests for it this week, I'll notify you in this issue thread when I get it done.
About the fix: please wait for the v0.22.38 , I have just fixed the pack testing which blocked the module publish.
@huan ok thanks. the mocker object initial twice, so the Contact pool init with empty. the bot can't reply successfully
As I mentioned in my last post: please use the v0.22.38 to get a fixed version of the puppet mock.
Message
, on the other side, the Mocker will always work with the MockMessage
.You can run this case, I don't know why Message Object Created unlimited
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
test('reply', async (t) => {
const mocker = new Mocker()
const puppet = new PuppetMock({ mocker })
const wechaty = new Wechaty({ puppet })
await wechaty.start()
const [user, contact] = mocker.createContacts(3)
await mocker.login(user)
// ATTENTION: you need to register your listener before you say anything
wechaty.on('message', async (message) => {
await message.say('world')
})
// how to write test case ? I want to get the reply message.
// SOLUTION:
let reply
contact.on('message', message => {
reply = message.text()
})
// contact say something to user
contact.say('Hello').to(user)
await sleep(1000)
await wechaty.stop()
// FINALLY!
t.isEqual(reply, 'world')
})
Oh, I know,just because emit a message to himself? wechaty say ('world') talker also receives the event?
Message Object Created unlimited
, could you please explain it in details?wechaty receives a message, and replies a message, then this message emits to (talker, room, listener) so wechaty receive this message again? then reply a message, once and once, is right? @huan
We have a createFixture
helper function now.
wechaty receives a message and replies a message, then this message emits to (talker, room, listener) so wechaty receive this message again? then reply a message, once and once, is right? @huan
No, that will not and should not happen.
You should never reply what said by yourself, that's why we need to do the follow code logic:
wechaty.on('message', message => {
if (message.self()) { return }
// to reply the message after the above code
})
ok, it works. thanks.
You are welcome.
As I promised, here's the unit test for Wechaty Vorpal Text Game: The Math Master, powered by the Wechaty Mocker:
contact send a message to bot. how to get the bot reply message?