I'm using TypeMock for the first time, so please slap me if I'm doing something I'm not supposed to do.
As tacky as it sounds, I've written a class that executes a command. Yes, I have to execute the command. I'm using System.Diagnostics.Process to do it. However, during UnitTest, I do not want the command to actually be executed (because it may not reside on the machine). I don't want my unit tests to fail simply because the machine they are being run on doesn't have this command installed.
So in comes TypeMock. I want to Mock the Process.Start() method such that it allows me to setup the Process object, but doesn't ever really start it.
And so.. here is my simplified code. I'm using notepad as an example app, it's not the command I'm actually trying to execute.
public void Exec()
{
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "notepad";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
}
And here is my TypeMock-based UnitTest (minus all the MS fluff) method:
[TestInitialize()]
public void MyTestInitialize()
{
MockManager.Init();
processMock = MockManager.Mock(typeof(System.Diagnostics.Process));
MockManager.LogFile = true;
processMock.Strict = false;
//processMock.ExpectCall("Start");
//processMock.ExpectCallAndVerify("Start");
// Uncomment the next two lines to use strict mode..
//processMock.Strict = true;
//processMock.ExpectGetAlways("StartInfo", new ProcessStartInfo());
processMock.AlwaysReturn("Start", null as Process);
}
[TestMethod()]
public void ExecTest()
{
Execute target = new Execute();
target.Exec();
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
ExpectCall("Start") throws an exception because Start doesn't return a void, it returns itself (an instance of Process). I happen to not care about the return in my method, so I can return null (question on that later...).
So if I do this.. notepad runs anyway. As if the Start() method is not being mocked.
I tried using strict and Mocked the constructor and StartInfo properties just fine. If I remove the mocking of Start, I get an except from TypeMock stating that there was an unexpected call to System.Diagnostics.Process.Start(). If I add the AlwaysReturn("Start", null as Process), I still get that error message.
Why is TypeMock ignoring me?
Here is the actual exception that I get when executing this UnitTest..
Test method TestProject1.ExecuteTest.ExecTest threw exception: TypeMock.VerifyException:
TypeMock Verification: Unexpected Call to System.Diagnostics.Process.Start().
Again that's when I use the strict mode. Can anyone show me what I am doing wrong?
Another question.. Should I care about the return code from Start, how would I tell TypeMock to return the mocked object instance as a System.Diagnostics.Process type? I can't seem to cast a MockObject into a Process object...
Thanks!
-Eric
________
NO2 VAPORIZER REVIEWS