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
Is it not possible to mock a property that is nested inside another one? For example:

Mock<Process> mockProcess = MockManager.Mock<Process>(Constructor.Mocked);

mockProcess.ExpectGet("StartInfo.Filename", "test");

I want to mock the Filename property of the StartInfo property to return test. How can that be done?
asked by repairman2003 (1.7k points)

2 Answers

0 votes
Hi
Sure you can do that but not through the mockProcess instance.
Just mock the ProcessStartInfo and expect the FileName property.

public void Test()
{
   Mock<ProcessStartInfo> mockStartInfo = MockManager.Mock<ProcessStartInfo>();
   mockStartInfo.ExpectGet("FileName", "test");

   Process proc = new Process();
   Assert.AreEqual(proc.StartInfo.FileName, "test");
   MockManager.Verify();
}


Or you can do it in natural mocks which is well... more natural :wink:

[Test]
public void Test()
{
   using(RecordExpectations recorder = RecorderManager.StartRecording())
   {
      Process mockProcess = new Process();
      recorder.ExpectAndReturn(mockProcess.StartInfo.FileName, "test");                
   }

   Process proc = new Process();
   Assert.AreEqual(proc.StartInfo.FileName, "test");
   MockManager.Verify();
}


Hope it helps. Please tell me if you have more questions.
answered by ohad (35.4k points)
0 votes
Ok, I see now. Thanks
answered by repairman2003 (1.7k points)
...