Mocked property still returns actual class

15 views
Skip to first unread message

Anthony Stevens

unread,
Dec 1, 2008, 11:53:52 PM12/1/08
to Moq Discussions
Hi - hope someone can help.

I'm creating a custom MSBuild task and want to test the Execute()
method after first mocking out the built-in Log property. Since Log
isn't virtual, I embedded it in a protected internal virtual property:

protected internal virtual Logger { get { return this.Log; } }

Here's my test:

[TestMethod]
public void Execute_1()
{
Mock<Microsoft.Build.Utilities.TaskLoggingHelper> log =
new Mock<Microsoft.Build.Utilities.TaskLoggingHelper>(It.IsAny<ITask>
());

Mock<RestoreDatabase> mock = new Mock<RestoreDatabase>();
mock.ExpectGet(x => x.Logger).Returns(log.Object);
mock.CallBase = true;

mock.Object.Execute();

}

Execute() calls this.Logger.LogMessage(MessageImportance.Low, "some
message");

I get an exception from the actual TaskLoggingHelper class, indicating
that my mocked log object is not fully mocked out. When the mocked
log object gets a LogMessage() method call I don't want to pass it
through to the actual TaskLoggingHelper class.

Can anyone advise?

Thanks,

a

Daniel Cazzulino

unread,
Dec 3, 2008, 5:57:37 PM12/3/08
to moq...@googlegroups.com
Does your RestoreDatabase use the Logger property?

Anthony Stevens

unread,
Dec 4, 2008, 10:49:17 AM12/4/08
to Moq Discussions
Of course. But when called via the test, it runs the actual
TaskLoggingHelper, and not my mocked out version.

a

On Dec 3, 2:57 pm, "Daniel Cazzulino" <dan...@cazzulino.com> wrote:
> Does your RestoreDatabase use the Logger property?
>
> On Tue, Dec 2, 2008 at 2:53 AM, Anthony Stevens
> <anthonyrstev...@gmail.com>wrote:

Daniel Cazzulino

unread,
Dec 12, 2008, 11:21:34 PM12/12/08
to moq...@googlegroups.com
I don't see anywhere that you're setting an expectation on the Mock<TaskLoggingHelper> to catch the calls to it... 

If LogMessage() is virtual on TaskLoggingHelper, you will be able to mock it. If it's not, creating a Mock<T> over it won't help.
Reply all
Reply to author
Forward
0 new messages