chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
OK - I found the problem. I just would like to know why it caused me so much heart-ache. This code compiles but it will not run - the Exception caused me a lot of grief also - italicized below. As soon as I change the FakeAnother property to a member instead of a property, everything works happily.

TypeMock.TypeMockException occurred
Message=" *** No method with name Setup>b__1 in type GenericBaseClassTest exists."


It looks like the "Setup>b__1 in type GenericBaseClassTest" has overwritten the real missing method name.

using System;
using System.Reflection;
using NUnit.Framework;
using TypeMock;
using TypeMock.ArrangeActAssert;

[TestFixture]
[ClearMocks]
public class GenericBaseClassTest : TestBaseClass<TestController>
{
    private IAnother _another;
    protected IAnother FakeAnother
    {
        get { return _another; }
        set { _another = value; }
    }

    [SetUp]
    public void Setup()
    {
        FakeAnother = Isolate.Fake.Instance<IAnother>(Members.ReturnRecursiveFakes);
        Isolate.WhenCalled(() => ServiceFactory.CreateInstance()).WillReturn(FakeSvcFactory);
        Isolate.WhenCalled(() => FakeSvcFactory.ReturnAnother()).WillReturn(FakeAnother);
        Isolate.WhenCalled(() => FakeAnother.GetObject()).WillReturn(null);
    }

    [Test]
    [Isolated]
    [VerifyMocks]
    public void Test1()
    {
        Controller.DoSomething();
    }
}
public interface IAnother
{
    object GetObject();
}

public class Another : IAnother
{
    public object GetObject()
    {
        return new object();
    }
}

public class ServiceFactory
{
    public static ServiceFactory CreateInstance()
    {
        return new ServiceFactory();
    }

    public IAnother ReturnAnother()
    {
        return new Another();
    }
}

public class TestBaseClass<C> where C : TestController
{
    protected F FakeSvcFactory;
    protected C Controller;

    protected TestBaseClass()
    {
        //create a factory instance
        FakeSvcFactory = Isolate.Fake.Instance<F>();
        Isolate.Fake.StaticMethods<F>(Members.ReturnRecursiveFakes);
        //create the controller
        ConstructorInfo ci = typeof (C).GetConstructor(new Type[] {typeof (F)});
        //inject the factory into the controller
        Controller = ci.Invoke(new object[] {FakeSvcFactory}) as C;
    }
}

public class TestController
{
    private ServiceFactory _sf;

    public TestController()
    {}

    public TestController(ServiceFactory sf)
    { _sf = sf; }

    public void DoSomething()
    {
        IAnother a = _sf.ReturnAnother();
    }
}
asked by kkilton (2.4k points)

6 Answers

0 votes
Hi

What happens is that in the line:
Isolate.WhenCalled(() => FakeAnother.GetObject()).WillReturn(null);

you are trying to fake the Test class (GenericBaseClassTest) property
and not IAnother.GetObject method as you intended.

:idea: If it's possible I would make the test code simpler. i.e
The test class would not be derived from base class and each test will create its own fakes.
I think that would make the test code easier to read and can
save you some grief :wink:
answered by ohad (35.4k points)
0 votes
Sorry about the double post.

I am trying to replicate the current code that I am testing (unsuccessfully) as best I can in portable code - hence the base classes and such.

I am ran into the same problem again in my real code. I am telling the service factory to return a faked object and it is not even after I removed the protected properties and replace them with protected variables.

Can you give me a correct example of how I can solve the problem that you are explaining?
you are trying to fake the Test class (GenericBaseClassTest) property
and not IAnother.GetObject method as you intended.


Thanks
answered by kkilton (2.4k points)
0 votes
So I think I understand a little better now after thumping my head on the desk. The problem is that I need to call a public method on a nonpublic property. Is this possible or would I be better off using one of the other APIs?
answered by kkilton (2.4k points)
0 votes
Actually, I was correct in the beginning. Turns out my REAL code was failing due to this bug...

https://www.typemock.com/community/viewt ... 43&forum=9
answered by kkilton (2.4k points)
0 votes
The bug described in post https://www.typemock.com/community/viewt ... 43&forum=9 was solved in Isolator version 5.1.1.

please download an update and see if it solves your problem.
answered by dhelper (11.9k points)
0 votes
Hi Kris

Sorry for the mistake.
The issue was solved after version 5.1.1.
I sent you a patch that should solve this issue.
answered by ohad (35.4k points)
...