简体   繁体   English

使用MvxTrace进行单元测试的代码

[英]Unit testing code with MvxTrace

I have this code: 我有以下代码:

    public async Task DeleteListAsync(Models.List list)
    {
        var userList = (await _userListRepository.GetAllAsync())
            .Where(ul => ul.ListId.Equals(list.Id, StringComparison.CurrentCultureIgnoreCase))
            .FirstOrDefault();

        if (userList != null)
            await _userListRepository.DeleteAsync(userList);
        else
            MvxTrace.Error(string.Format("Unable to find UserList entry for List {0}.", list.Id));

        await _listRepository.DeleteAsync(list);
    }

The question I have is as follows - how do I unit test this code with a call to MvxTrace which is part of MvvmCross framework. 我的问题如下-我如何通过调用MvvmCross框架的一部分MvxTrace来对该代码进行单元测试。 I know that I can wrap a call into MvxTrace into my own interface base method and provide own implementation for unit test purposes but is there a better/different way to provide a mock implementation for MvxTrace without the effort of wrapping it? 我知道我可以将对MvxTrace的调用包装到我自己的接口基本方法中,并为单元测试目的提供自己的实现,但是是否有更好/不同的方法为MvxTrace提供模拟实现而无需进行包装?

If you are using Mvx 5.x or greater you shouldn't use MvxTrace anymore and use IMvxLog instead, then you can inject it in the constructor and do the unit tests easier. 如果您使用的是Mvx 5.x或更高版本,则不应再使用MvxTrace并改用IMvxLog ,然后可以将其注入构造函数中,并使单元测试更加容易。

If not you have to create your own implementation of IMvxTrace and register it because as you can see here ( Mvx 4.4.0 implementation of MvxTrace ) MvxTrace resolves IMvxTrace that is what the static methods call at the end. 如果不是,则必须创建自己的IMvxTrace实现并注册它,因为如您在此处看到的( MvxTrace的Mvx 4.4.0实现MvxTrace解析IMvxTrace ,这是静态方法最后调用的内容。

Create custom MvxTrace : 创建自定义MvxTrace

public class MyTestableMvxTrace : IMvxTrace
{
    // Implement the interface adding custom flags
    // or data so that you can use it in the assert
    ...
}

Register it in your test: 在测试中注册:

Mvx.RegisterType<IMvxTrace, MyTestableMvxTrace>();

HIH HIH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM