1

In TypeMock you can create a future mock object, for example:

public class ClassToTest
{   
    public ClassToTest()
    {
        var o = new Foo();
    }
}

[Test]
public void Test()
{
    var fakeFoo = Isolate.Fake.Instance<Foo>();

    Isolate.Swap.NextInstance<Foo>().With(fakeFoo);
}

Does MS Fakes have the same functionality as the above?

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • As far as I know this is not supported. Fakes still provides powerful stubbing and mocking features but I don't think it is there yet. – Spock Nov 28 '13 at 21:19
  • Yeah not looking promising :( MS Fakes is a nice alternative to open source mocking frameworks, but I still hold TypeMock as the number 1 mocking tool I've ever used. – Jason Evans Nov 28 '13 at 22:04
  • 1
    Actually, if I'm reading what you're doing here correctly, you can probably do this with a shim. Still, it requires the test to know that some class has a specific dependency that isn't part of it's contract, which isn't really good design. – Magus Dec 02 '13 at 21:16
  • Ahh OK. I know about shims a bit, but not enough to know whether they can achieve what I'm looking for. I'll do some digging around, thanks for the tip. – Jason Evans Dec 03 '13 at 08:18
  • @Magus Found this - http://stackoverflow.com/questions/14916162/how-do-i-get-shims-for-base-classes-using-microsoft-fakes – Jason Evans Dec 03 '13 at 08:19

1 Answers1

1

I found a great example from this SO question which demonstrates how to fake future instances of objects. Here's an example from that question:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        ClassLibrary1.Child myChild = new ClassLibrary1.Child();

        using (ShimsContext.Create())
        {
            ClassLibrary1.Fakes.ShimChild.AllInstances.addressGet = (instance) => "foo";
            ClassLibrary1.Fakes.ShimParent.AllInstances.NameGet = (instance) => "bar";

            Assert.AreEqual("foo", myChild.address);
            Assert.AreEqual("bar", myChild.Name);
        }

    }
}

This looks like it will do the trick for me.

Community
  • 1
  • 1
Jason Evans
  • 28,906
  • 14
  • 90
  • 154