This project is read-only.

MockStyle.Transparent not working?

Apr 10, 2012 at 8:30 PM
Edited Apr 11, 2012 at 4:42 PM

I wanted to ask this publicly before opening up an issue, but it appears to me that MockStyle.Transparent is not working as designed.  I'm currently using it to mock a class, then stub out single functions of that class (tried with and without the abstract modifier on the class).  Each function I'm stubbing is public, and I've tried with and without the virtual modifier.

The problem I'm seeing is that even after stubbing a method, the underlying implementation is still executed.  The return value, however, is the one specified in my Stub.Out.Method command.  So what I observe is that my tests pass, but the underlying implementation throws all kinds of errors when it's executed.  Here's a simple example:

MockFactory mocks;
Mock<Database> dbMock = mocks.CreateMock<Database>(MockStyle.Transparent);
dbMock.Stub.Out.Method(x => x.WriteData(null)).WithAnyArguments().WillReturn("customReturnVal");
Assert.AreEqual("customReturnVal", dbMock.WriteData(null));

The test passes, but the actual implementation of WriteData(null) is executed!  Is there something I'm likely to be missing?  Please let me know if more info is desired, thanks.