简体   繁体   English

类型为DateTimeOffset的模拟属性导致Windows Store测试库中的InvalidProgramException

[英]Mocking property of type DateTimeOffset results in InvalidProgramException in a Windows Store Test library

I just started out with a Windows Store application and deciced to use test-driven development in my project. 我刚开始使用Windows Store应用程序,并决定在我的项目中使用测试驱动的开发。 Since I've had some experience with NUnit previously that was my library of choice, and then I started using MoqRT which was the mocking library recommended by a website as the mocking library to use for Windows Store applications. 由于我之前曾对NUnit有所了解,因此我选择了它作为库,然后我开始使用MoqRT,这是网站推荐的模拟库,用作Windows Store应用程序的模拟库。

The problem I am facing is when I'm mocking an object that has a DateTimeOffset as the type of the property. 我面临的问题是当我模拟具有DateTimeOffset作为属性类型的对象时。 I receive the following exception: 我收到以下异常:

System.InvalidProgramException: Common Language Runtime detected an invalid program System.InvalidProgramException:公共语言运行时检测到无效程序
at Castle.Proxies.ITimeProxy_1.get_Expires() 在Castle.Proxies.ITimeProxy_1.get_Expires()

... the rest of stacktrace removed due to clarity ... 由于清晰起见,其余的stacktrace已删除

Now, this only occurs when I use the DateTimeOffset type on the property, and mocking for instance a string property works perfectly fine. 现在, 只有当我在属性上使用DateTimeOffset类型时, 才会发生这种情况,例如对字符串属性进行模拟就可以了。

using System;
using Moq;
using NUnit.Framework;

namespace StoreTesting
{
    [TestFixture]
    public class UnitTest1
    {
        [Test]
        public void TestMethod1()
        {
            // Mock
            DateTimeOffset expirydate = DateTimeOffset.UtcNow.AddSeconds(2000);

            var time = new Mock<ITime>();
            time.Setup(m => m.Expires).Returns(expirydate);

            // Act
            TimeDependant obj = new TimeDependant(time.Object);
            var result = obj.Act();

            // Assert
            Assert.That(result, Is.EqualTo(expirydate));
        }
    }

    public class TimeDependant
    {
        private readonly ITime time;

        public TimeDependant(ITime time)
        {
            this.time = time;
        }

        public DateTimeOffset Act()
        {
            return time.Expires;
        }
    }

    public interface ITime
    {
        DateTimeOffset Expires { get; }
    }
}

If I subclass ITime and use that instead of the Mock, everything is just fine as well. 如果我ITime并使用它代替Mock,那么一切也很好。

When searching for this it seems as though the main problem of the exception is related to "too large methods", but seeing as this only occurs for properties of type DateTimeOffset I don't see how that can affect my test, since other properties should fail as well if it was something in regards to that. 在搜索此内容时,似乎异常的主要问题与“太大的方法”有关,但是看到这种情况仅发生在DateTimeOffset类型的属性上,我看不到它会如何影响我的测试,因为其他属性应该如果这与之有关,那么也会失败。

Another thing is that I'm running the tests using the NUnit application, as the tests won't run in Visual Studio 2012 due to it being a Windows Store application (which is a whole other question). 另一件事是,我正在使用NUnit应用程序运行测试,因为由于它是Windows Store应用程序,因此该测试无法在Visual Studio 2012中运行(这是另一个问题)。 I don't know if this can affect the behaviour as well. 我不知道这是否也会影响行为。

The question I'm asking is; 我要问的问题是; Is there a way to mock interfaces with properties of type DateTimeOffset without having it throwing an exception? 有没有一种方法可以模拟具有DateTimeOffset类型的属性的接口而又不会引发异常?

You cannot use Moq with Windows Store Runtime as I know, because winrt doesn't allow dynamic codegen . 据我所知,您不能在Windows Store Runtime中使用Moq,因为winrt不允许动态codegen

I do it just thought creating simple .NET Framework library, which reference all my code, which I want to test, from Win Store Project 我只是想创建一个简单的.NET Framework库,该库引用了Win Store Project中要测试的所有代码。

How to use Moq and nUnit to write Unit Tests for Windows Runtime libraries 如何使用Moq和nUnit编写Windows运行时库的单元测试

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

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