使用MvxIoCSupportingTest时如何让MvxTrace发出消息

时间:2013-12-14 22:28:43

标签: c# mvvmcross

public class PathTests : MvxIoCSupportingTest

我有

    [Test]
    public void GetTemp()
    {
        var temp = Path.GetTempPath();
        MvxTrace.Trace("hello from Mvx");
        Trace.WriteLine("hello from Trace");
        Debug.WriteLine("hello from Debug");
        Assert.NotNull(temp);
    }

但是,Mvx的测试输出中没有显示任何内容。输出是

  

hello from Debug hello from Debug

(在上面的示例中,Trace来自System.Diagnostics。)在http://blog.fire-development.com/2013/06/29/mvvmcross-enable-unit-testing/中它看起来像

_ioc.RegisterSingleton<IMvxTrace>(new TestTrace());

我错过了什么?

更新。我正在使用NCrunch,这是整个代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Loqu8.MvvmCross.Plugins.IO;
using Cirrious.MvvmCross.Test.Core;
using Cirrious.CrossCore.Plugins;
using Cirrious.CrossCore.Platform;
using Cirrious.CrossCore;
using System.Diagnostics;

namespace IO.Wpf.Tests
{

    [TestFixture]
    public class PathTests : MvxIoCSupportingTest
    {    
        public PathTests()
        {
            Setup();
            Ioc.RegisterSingleton<IMvxPath>(new MvxPath());
            Ioc.RegisterSingleton<IFileSystem>(new DesktopFileSystem());
            Ioc.RegisterType<IFile, FileSystemFile>();
            Ioc.RegisterType<IFolder, FileSystemFolder>();
        }

        [Test]
        public void GetTemp()
        {
            var temp = Path.GetTempPath();
            MvxTrace.Trace("hello from Mvx");
            Trace.WriteLine("hello from Trace");
            Debug.WriteLine("hello from Debug");
            Assert.NotNull(temp);
        }
    }
}

和输出:

enter image description here

1 个答案:

答案 0 :(得分:2)

如果我将测试添加到http://blog.fire-development.com/2013/06/29/mvvmcross-enable-unit-testing/,那么我会看到输出:

mvx: Diagnostic:   0.01 hello from Mvx
hello from Trace
hello from Debug

完整的测试代码:

[TestFixture]
public class MvvmCrossTestSetup
{
    private IMvxIoCProvider _ioc;

    protected IMvxIoCProvider Ioc
    {
        get { return _ioc; }
    }

    public void Setup()
    {
        ClearAll();
    }

    protected void ClearAll()
    {
        // fake set up of the IoC
        MvxSingleton.ClearAllSingletons();
        _ioc = MvxSimpleIoCContainer.Initialise();
        _ioc.RegisterSingleton(_ioc);
        _ioc.RegisterSingleton<IMvxTrace>(new TestTrace());
        RegisterAdditionalSingletons();
        InitialiseSingletonCache();
        InitialiseMvxSettings();
        MvxTrace.Initialize();
    }

    protected void InitialiseMvxSettings()
    {
        _ioc.RegisterSingleton<IMvxSettings>(new MvxSettings());
    }

    protected virtual void RegisterAdditionalSingletons()
    {
    }

    private static void InitialiseSingletonCache()
    {
        MvxSingletonCache.Initialise();
    }

    [Test]
    public void GetTemp()
    {
        Setup();

        var temp = Path.GetTempPath();
        MvxTrace.Trace("hello from Mvx");
        Trace.WriteLine("hello from Trace");
        Debug.WriteLine("hello from Debug");
        Assert.NotNull(temp);
    }

    private class TestTrace : IMvxTrace
    {
        public void Trace(MvxTraceLevel level, string tag, Func<string> message)
        {
            Debug.WriteLine(tag + ":" + level + ":" + message());
        }

        public void Trace(MvxTraceLevel level, string tag, string message)
        {
            Debug.WriteLine(tag + ": " + level + ": " + message);
        }

        public void Trace(MvxTraceLevel level, string tag, string message, params object[] args)
        {
            Debug.WriteLine(tag + ": " + level + ": " + message, args);
        }
    }
}

使用Resharper Test Runner测试运行

  

我错过了什么?

不知道 - 我只是按照http://blog.fire-development.com/2013/06/29/mvvmcross-enable-unit-testing/中的代码并将其与您包含的代码相结合。也许尝试将您的代码与该博客的代码进行比较?或者在此发布您的完整样本,以便其他人可以比较它?