I'm using TypeMock 3.7.1.0 on .NET 1.1.
It seems that when I set up an expect-and-return using Natural Mocks on a static method, the first time I try setting up the expectations it's actually calling the static method, which is causing my tests to fail in odd and unfortunate manners.
For example, in my test [SetUp] I might have something like:
SomeObject returnValue = new SomeObject();
using(RecordExpectations recorder = RecorderManager.StartRecording())
{
recorder.ExpectAndReturn(ServiceClass.GetSomeValue(), returnValue);
recorder.RepeatAlways();
}
And the ServiceClass might look like this:
public sealed class ServiceClass
{
private static readonly LoggingClass logger = LoggingClassFactory.GetLogger();
public static SomeObject GetSomeValue()
{
logger.Log("Going to return a value now.");
return new SomeObject();
}
}
If I execute a test, the test will fail because when I'm setting up expectations, the GetSomeValue method will actually execute, the "logger" static variable won't be initialized, and I'll get a NullReferenceException.
Interestingly enough, this will only happen for the first test that executes - on subsequent tests, this passes (but ostensibly because the static "logger" has been initialized).
Unfortunately I tried to put together a simple reproduction but I don't seem to be able to reproduce it outside of the [actually fairly large] project I'm working in.
I have many, many other tests using TypeMock and this is the only instance I'm seeing this issue, and it's only when mocking statics.
I've had other developers try it and it's failing on other machines, including our build server, so it's not environmental.
We do have a fairly complex series of dependencies with this project and have several strong-named references that are getting worked out at runtime using app.config files with bindingRedirects specified. I don't know if this affects how TypeMock might deterine what to mock, but this seemed pretty special-case to me, which might be why I can't put together a separate reproduction.
These tests worked with no issues under TypeMock 3.6.0.0. I only encountered it when I upgraded to 3.7.1.0. I don't know if there's some difference in how statics are handled between the two.
Unfortunately, if I can't get this resolved reasonably soon, I'm going to have to revert back to 3.6.0.0, which isn't something I want to do.
Let me know if I can answer any additional questions or try anything out to help diagnose.