The idea of the code is to mock the TestBig.Method() to return a filled TestObject when it's called from Controller.Start(). And TestObject has mostly read-only properties which would be filled by the TestBig.Method().
I understand that you want to mock the call to TestBig.Method() and return a mocked TestObject. Since TestObject has no real implementation you will need to use a mock instead, mocking all calls made on him during the execution of the test.
here is a way to do it: (without the expectation on TestObject)
using (RecordExpectations rec = new RecordExpectations())
{
TestObject mockedTestObject = RecorderManager.CreateMockObject(typeof(TestObject));
TestBig mockedTestBig = RecorderManager.CreateMockObject(typeof(TestBig ));
rec.ExpectAndReturn(mockedTestBig.Method(),mockedTestObject);
}
The reflective mocking below works and a filled TestObject is returned by the mocked TestBig.Method(). The natural mocking didn't work though: the code threw an exception when I was trying to set a value to the read-only property "Property".
This shouldnt happen what was the exception that you were getting? and where exactly were you getting it (I'm not sure if you got it when trying to mock the property or in the recording block see below)
I guess I'm trying to figure out when and how to use the reflective way and when the natural.
Natural and Reflective are basically two different ways to achieve the same. Some people find natural easier to understand and work with and some reflective. Basically there are some things that you can achieve only using reflective such as mocking private methods and such, but using natural has the advantage of strong typing and is eaiser to maintain later using common refactoring tools.
The real code has a method which returns a complex object structure with many read-only properties. And we want to make sure that when mocking a call to that method, the returned object is generated in a single location and the delegate seems like a way to go. Moving the mocked object creation to a separate method means less duplicate work and the original code remains cleaner.
Naturally, but you dont need to use a delegate for this. You can just move the creation to a regular method. I would even say to store the created mock into a memebr variable using it as a return value later on when needed. something in the spirit of:
void MockCreation()
{
//create
mockedTestObject = MockManager.MockObject(typeof(TestObject));//where mockedTestObject is a memebr variable
//now need to setup needed expectations
}
void TestMethod()
{
using (RecordExpectations rec = new RecordExpectations())
{
TestBig mockedTestBig = RecorderManager.CreateMockObject(typeof(TestBig ));
rec.ExpectAndReturn(mockedTestBig.Method(),mockedTestObject);
}
}
:idea: Most testing frameworks has a setup mechanims which allows you to run a given method at the start of each test
I know it's vague. NDA's... But hopefully that cleared at least some of it.
If you like we can take if offline (and even sign an NDA if needed.)
using (RecordExpectations recorder = new RecordExpectations())
{
testBig.Method();
recorder.ExpectAndReturn(0, new DynamicReturnValue(BuildTestObject));
}
This piece of code is not right
it should be:
using (RecordExpectations recorder = new RecordExpectations())
{
testBig.Method();
recorder.Return(new DynamicReturnValue(BuildTestObject));
}
or
using (RecordExpectations recorder = new RecordExpectations())
{
recorder.ExpectAndReturn(testBig.Method(), new DynamicReturnValue (BuildTestObject));
}
the way it is written you'll get an exception (you actually record two calls with only one return value)
Hope this help
let me know if you have any more questions