Igor
Igor

Reputation: 609

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 a s follows - how do I unit test this code with a call to MvxTrace which is part of MvvmCross framework. 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?

Upvotes: 1

Views: 107

Answers (1)

fmaccaroni
fmaccaroni

Reputation: 3916

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.

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.

Create custom 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

Upvotes: 1

Related Questions