机器假货"WithFakes has not been initialized yet. Are you calling it from a static initializer?"错误



将Machine.Fakes从1.0.1更新到1.7版本后,我收到一个"WithFakes尚未初始化。您是从静态初始化器调用它吗?"错误/异常。

我的测试结构是这样的:

[TestFixture]
public class MailSenderTests : WithSubject<MailSender>
{
    [TestFixture]
    public class TheSendMethod : AssertionHelper
    {
        [Test]
        public void Test_that_exception_is_thrown_if_no_recievers()
        {
            Expect(() => Subject.Send(string.Empty, string.Empty, recievers: null), Throws.InstanceOf<ArgumentException>());
        }
    }
}

我在SUT中测试的每个方法都有一个类。

有人能告诉我我做错了什么吗?

您没有使用Machine。按预期方式伪造。它是Machine.Specifications的扩展,没有它就没有意义。您在代码示例中使用了其他一些测试框架。这种不兼容性与版本无关,除了引入了明确的错误消息。

扩展shamp00的答案:

using System;
using Machine.Fakes;
using Machine.Specifications;
namespace SOAnswers
{
    [Subject(typeof(MailSender), "Sending an Email")]
    public class When_no_receivers_are_specified : WithSubject<MailSender>
    {
        static Exception exception;
        Because of = () =>
            exception = Catch.Exception(() => Subject.Send(string.Empty, string.Empty, receivers: null));
        It should_throw_an_exception = () =>
            exception.ShouldBeOfType<ArgumentException>();
    }
}

我觉得这很有表现力然而,如果你不想使用Machine.Specification,我想你应该寻找一个更适合的自动锁定框架。

我想自1.0.1版本以来发生了很多变化。对于1.7.0版本,您的测试应该看起来像这个

public class Given_a_MailSender : WithSubject<MailSender>
{
    static Exception Exception;
    Because of = () =>
    {
        Exception = Catch.Exception(() => Subject.Send(string.Empty, string.Empty, receivers: null));
    };
    It should_throw_an_exception = () => Exception.ShouldBeOfType<ArgumentException>();
}

相关内容

最新更新