0

我需要在 Windows Phone 中编写一个单元测试来测试我的数据是否被反序列化为正确的类型。这是我到目前为止所做的。

[TestMethod]
    [Asynchronous]
    public void SimpleTest()
    {
        await pots = help.potholes();

我收到一条错误消息,说“罐子”不可等待。Pots 是一个列表,它应该接受来自对我的 web 服务进行异步调用的 potholes 函数的结果。

这是使用 Restsharp 进行调用的方法。

public void GetAllPotholes(Action<IRestResponse<List<Pothole>>> callback)
    {

        var request = new RestRequest(Configuration.GET_POTHOLE_ALL,Method.GET);
        request.AddHeader("Accept", "application/json");
        _client.ExecuteAsync(request, callback);

    }

我怎样才能让花盆可以等待?在 Windows Phone 中测试休息服务的正确方法是什么?

我正在使用 Windows Phone 工具包测试框架

这是我正在关注的教程。 异步测试

4

3 回答 3

1

“异步”一词现在在 .net 中被重载。

您引用的文章指的是awaitable方法,而不是通过回调异步的方法。

这是一个关于如何测试它的粗略想法。

[TestMethod]        
[Asynchronous]
public void SimpleTest()
{
    // set up your system under test as appropriate - this is just a guess
    var help = new HelpObject();

    help.GetAllPotholes(
        response =>
        {
            // Do your asserts here. e.g.
            Assert.IsTrue(response.Count == 1);

            // Finally call this to tell the test framework that the test is now complete
            EnqueueTestComplete();
        });
}
于 2013-03-07T15:42:55.823 回答
1

正如 matt 所表达的,术语“异步”现在在多个上下文中使用,在 Windows Phone 上的测试方法的情况下,正如您在代码中看到的那样,它不是关键字,而是一个属性,其目标是释放工作线程到允许其他进程运行,并让您的测试方法等待 UI 或服务请求中可能发生的任何更改。

你可以做这样的事情来让你的测试等待。

[TestClass]
public class ModuleTests : WorkItemTest
{
    [TestMethod, Asynchronous]
    public void SimpleTest()
    {
        var pots;
        EnqueueDelay(TimeSpan.FromSeconds(.2)); // To pause the test execution for a moment.
        EnqueueCallback(() => pots = help.potholes());
        // Enqueue other functionality and your Assert logic
        EnqueueTestComplete();
    }
}
于 2013-03-08T17:33:28.633 回答
0

您正在以错误的方式使用 async .. await

试试这个

public async void SimpleTest()
{
    pots = await help.potholes();
    ....
}
于 2013-03-07T07:58:09.213 回答