小编Shi*_*nbo的帖子

如何测试异步void方法

请考虑以下代码.通过调用GetBrands,将为属性Brands分配适当的数据.

public class BrandsViewModel : ViewModelBase
{
    private IEnumerable<Brand> _brands;
    public IEnumerable<Brand> Brands
    {
        get { return _brands; }
        set { SetProperty(ref _brands, value); }
    }

    public async void GetBrands()
    {
        // ......

        Brands = await _dataHelper.GetFavoriteBrands();

        // ......
    }
}
Run Code Online (Sandbox Code Playgroud)

但如果我按如下所示进行测试,则测试失败.如何在方法GetBrands中等待异步调用?

[TestMethod]
public void AllBrandsTest()
{
    BrandsViewModel viewModel = new BrandsViewModel();
    viewModel.GetBrands();
    Assert.IsTrue(viewModel.Brands.Any());
}
Run Code Online (Sandbox Code Playgroud)

c# unit-testing async-await

3
推荐指数
1
解决办法
1766
查看次数

现代C++中的自动生命周期管理无法按预期工作

我试图证明现代C++一旦超出范围就会自动删除它.我正在使用下面的代码来执行测试.但它并没有像预期的那样真正起作用.根据任务管理器中显示的内存大小,它仍然具有200 + MB内存.但是一旦我取消注释delete stringTest,内存减少到不到1 MB.有人请帮忙看看我在这里忽略了什么吗?非常感谢.

在我的测试中使用了Visual Studio 2015.

#include "stdafx.h"
#include <iostream>

class StringTest
{
public:
    std::string StringSample;

    StringTest::StringTest()
    {
        StringSample = "abcdefghijklmnopqrstuvwxyz...";
    }

    std::string StringTest::Substring(int length)
    {
        std::string result = StringSample.substr(0, length);

        return result;
    }
};

void testStringSeveralTimes()
{
    for (auto i = 0; i < 100000; i++)
    {
        auto stringTest = new StringTest();
        // delete stringTest;
    }
}

int main()
{
    testStringSeveralTimes();

    std::cout << "Done." << std::endl;
    int a;
    std::cin >> a;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

-2
推荐指数
1
解决办法
80
查看次数

标签 统计

async-await ×1

c# ×1

c++ ×1

unit-testing ×1