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
0 votes
Hi,
I have a class which implements two different interfaces.
There is a second class which receives an object of the previous class and performs a cast to one
of the interfaces implemented by the class.

In my tests I've mocked an object of TypeA and called the method on TestB using my mocked object as parameter.
Inside the method there is a cast to ITypeB but this cast returns an object with all its members set to null, so the TestB.Methdod() throws an exception.

Maybe this simple code will clarify the case:
public interface ITypeA
{
   public int attributeA{get;}
}
public interface ITypeB;
{
   public int attributeB{get;}
}

public class TestA: ITypeA,ITypeB
{
   .... methods & attributes
}

public class TestB
{
   public void Method(TestA objA)
   {
      ITypeB b = objA as ITypeB);
      int value = b.attributeB; // This throws an exception.
   }
}


And the code used to test:

TestA fakeTestA = Isolate.Fake.Instance<TestA>();
Isolate.WhenCalled(() => fakeTestA.attributeA).WillReturn(1);
Isolate.WhenCalled(() => fakeTestA.attributeB).WillReturn(2);
TestB realTestB = new TestB();
realTestB.Method(fakeTestA);


Thanks in advance,
asked by rsaladrigas (1.8k points)

3 Answers

0 votes
Hi Roger,

I'm a bit confused so help me a bit.

Interfaces cannot contain fields, just properties and methods. Now, running with both methods and properties, your test here runs just fine.

However, you may be seeing something else. If one of the fields in the class is null, that's because the constructor of the class was not called. In order to do that, you need to call fake like this:

TestA fakeTestA = Isolate.Fake.Instance<TestA>(Members.CallOriginal);


In this case, if TestA had a field x, which is initialized in the constructor, it will have that value. If not, it's going to be 0 (or null if reference type).

Let me know if that helps,
answered by gilz (14.5k points)
0 votes
Hi Gilz,
Thanks for your answer.
I've already tried the Members.CallOriginal, but it throws an Exception because it expects a DevExpress.Xpo.Session as a parameter to the constructor (and it can't be null!)

I don't know how to pass a parameter to the constructor using the new AAA API.

As for the interfaces, they do contain properties. Sorry if my quick pseudo-c# sample was confusing.
answered by rsaladrigas (1.8k points)
0 votes
Hi Roger,

Ah - got us there.
That feature is not implemented - currently we don't have. Yet. But it is on our feature list. We'll keep you updated when we've got it out.

Thanks again,
answered by gilz (14.5k points)
...