3

我已经阅读了 AppEngine单元测试指南,并且我得到了 Java 数据存储服务测试,但我对 Channel 服务没有任何运气。该指南没有提供任何有关通道测试的具体示例,javadocs 也没有太大帮助,但是我的 IDE 向我展示了一些似乎用于对本地通道服务进行单元测试的类;我只是不知道如何使用它们。

有没有人有任何测试 GAE 渠道服务的经验或示例?

4

3 回答 3

2

以下对我有用:

import unittest

from google.appengine.api import channel
from google.appengine.ext import testbed


class TestCase(unittest.TestCase):

  def setUp(self):
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_channel_stub()

  def test_send(self):
    channel_stub = self.testbed.get_stub('channel')
    token = channel.create_channel('ClientID1')
    channel_stub.connect_channel(token)
    channel.send_message('ClientID1', 'hello')
    channel_messages = channel_stub.get_channel_messages(token)
    channel_stub.clear_channel_messages(token)
    self.assertEquals(['hello'], channel_messages)


if __name__ == '__main__':
  unittest.main()

您还可以查看通道服务存根的源代码

于 2012-06-25T16:02:34.203 回答
1

Ivan 提供了简洁的 Python 示例,这是 Java 的解决方案。

假设这是我们要测试的代码

public class ClientChannelService {

    private ChannelService channelService = ChannelServiceFactory.getChannelService();

    public String createToken(){
        return channelService.createChannel(UUID.randomUUID().toString());
    }

    public void sendMessage(String token, String message){
        channelService.sendMessage(new ChannelMessage(token, message));
    }
}

首先,添加appengine-testing.jar到类路径:

<dependency>
   <groupId>com.google.appengine</groupId>
   <artifactId>appengine-testing</artifactId>
   <version>${appengine.version}</version>
   <scope>test</scope>
</dependency>

接下来,创建一个测试,如下所示。我在这里假设 JUnit,但通常你可以自由使用任何测试框架,这对 GAE 没关系。

private ClientChannelService service;
private LocalServiceTestHelper helper = new LocalServiceTestHelper(
                                            new LocalChannelServiceTestConfig());
private ChannelManager channelManager;

@Before
public void setUp() {
    helper.setUp();
    channelManager = LocalChannelServiceTestConfig.getLocalChannelService()
                                                  .getChannelManager();
    service = new ClientChannelService();
}

@After
public void tearDown() {
    helper.tearDown();
}

@Test
public void testSendMessage() {
    String token = service.createToken();
    connectionId = channelManager.connectClient(token); //emulate client connection

    service.sendMessage(token, "message");

    String message = channelManager.getNextClientMessage(token, connectionId);
    assertEquals("message", message);
}
于 2013-11-22T14:57:35.567 回答
0

您是否在http://code.google.com/p/googleappengine/source/browse/trunk/java/src/main/com/google/appengine/tools/development/testing/LocalChannelServiceTestConfig.java?r= 182

哎呀对不起上一篇文章,我意识到你没有做能力......

于 2012-06-23T22:00:54.153 回答