Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

mocked property is getting ignored

+2 votes
Does anyone know what could cause this to show up at the top of a stack trace?

at TypeMock.MethodDecorator.CallRealMethod()
at TypeMock.MethodDecorator.a()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)

It's getting called in my unit test method, and then goes on to ignore the static property that I have mocked.

I can post some of my code if necessary, but I'm guessing it's one of those really obvious things that people will just know.
asked Jan 10, 2009 by smitty3268 (460 points)

7 Answers

0 votes
Hi Smitty

Sorry the stack trace is not very helpful in this case :(
Please post the test code and the exception message you see.
answered Jan 10, 2009 by ohad (37,080 points)
+2 votes
Well, this is what my code looks like:

//mock ServiceFactory<T>.Instance, a static property which returns an object of type T (and caches it after the 1st time when it gets created)

MockObject mockService = MockManager.MockObject<IMyCustomService>();
Mock mockFactory = MockManager.MockAll<ServiceFactory<IMyCustomService>>();
mockFactory.ExpectGetAlways("Instance", mockService.Object);

//mock all the methods in the service class
mockService.AlwaysReturn("MyCustomMethod", true);

//call the method in my business logic
ServiceFactory<IMyCustomService>.Instance.MyCustomMethod("", "");

//which throws the exception - a null reference exception that occurs inside the Instance property because it can't create the object since it expects it will be mocked instead.
answered Jan 12, 2009 by smitty3268 (460 points)
+2 votes
And at a higher level:

[TestFixture, VerifyMocks]
public class MyTest {

[Setup]
public void Setup() {

MockManager.Init();
if(!MockManager.IsTypeMockedAll<ServiceFactory<IMyCustomService>>()) {
//create the mocked objects, setup expectations using ExpectGetAlways and AlwaysReturn.
}

}

[Test]
public void TestMethod() {
//call the ServiceFactory<T>.Instance property which is not mocked correctly
}

}
answered Jan 12, 2009 by smitty3268 (460 points)
+2 votes
Hi Smitty,

Seem like I will need a code I can compile and run to solve this issue.

Lets take it offline.
answered Jan 12, 2009 by dhelper (23,700 points)
+2 votes
My work email is blocking zip files :( but I can send you a test project tonight. I've managed to simplify the test code, though, so hopefully you can reproduce it with just the code listed below.

This throws the “not mocked” exception during the test:

[TestFixture, VerifyMocks]
public class FilterTests
{
[SetUp]
public void Setup()
{
MockManager.Init();
Mock test = MockManager.MockAll<Test>();
test.ExpectGetAlways("TestProperty", 1);
}

[Test]
public void MinimalTest()
{
int t = Test.TestProperty;
Assert.AreEqual(t, 1);
}

}

public class Test
{
public static int TestProperty
{
get { throw new Exception("not mocked!"); }
}
}
answered Jan 12, 2009 by smitty3268 (460 points)
+2 votes
I think I've figured out the problem - I saw that static method support was only added in version 5.1, so I double checked and my project was still referencing the 5.0 version of the dll. I pointed it to the new 5.1.2 and I think it's now working.

A quick question - is it recommended to update to the new API even for 2.0 projects (example here: http://blog.typemock.com/2008/10/how-to ... s2005.html) or is the old API still fine?
answered Jan 13, 2009 by smitty3268 (460 points)
+2 votes
I'm glad you worked things out.

Needless to say that the existing functionality of NAtural and Reflective APIs (old version) still exist and can be used.

The new AAA interface is based on using Lambda expressions which do not exist in .NET 2.0 but you can using anonymous delegates instead.

Soon when out new VB.NET API comes out it would not require using Lambda and could be used from C# .NET 2.0 as well!
answered Jan 13, 2009 by dhelper (23,700 points)
...