0

After updating Machine.Fakes to version 1.7 from 1.0.1 I am getting a "WithFakes has not been initialized yet. Are you calling it from a static initializer?" error/exception.

I am structering my tests like this:

[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>());
        }
    }
}

I have a class for each method I am testing in the SUT.

Can someone tell me what I am doing wrong?

shamp00
  • 11,106
  • 4
  • 38
  • 81
Oksen
  • 15
  • 2
  • To make it work in the above scenario where nunit is used as test framework. This helped me :-) [TestFixtureSetUp] public void Set() { _specificationController = new SpecificationController(); } – Oksen Sep 09 '13 at 11:55

2 Answers2

0

Well I guess a lot has changed since version 1.0.1. With version 1.7.0, your test should look like this

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>();
}
shamp00
  • 11,106
  • 4
  • 38
  • 81
0

You are not using Machine.Fakes the way it is intended. It is an extension of Machine.Specifications and does not make sense without it. You are using some other test framework in your code example. This incompatibility has nothing to do with the version - apart from the explicit error message that has been introduced.

To expand on shamp00's answer:

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>();
    }
}

I think this is very expressive. :-) However, if you don't want to use Machine.Specifications, I suppose you should look for an automocking framework that fits better.

Simon Hohenadl
  • 502
  • 3
  • 8
  • Ok thank you for the answer. I understand that now it’s not possible to only use the nice faking part. Your problem is that there are lots of tests where the machine.specification part is not used. We will roll back and perhaps rewrite the tests later :-) – Oksen Sep 09 '13 at 09:01
  • CORRECTION: The exception you get was actually introduced unintentionally. Your usage of WithSubject should again be possible with version 2.0 of Machine.Fakes - this doesn't mean it's recommended or will be supported in the future however. – Simon Hohenadl Dec 27 '13 at 11:26