DevelopMENTAL Madness

Wednesday, March 11, 2009

Moq: Missing Expect methods (Expect, ExpectGet, ExpectSet)

I got stuck on a really stupid problem yesterday and I just figured it out as I was writing a post on the Moq forums. It seems really dumb and maybe that's why I couldn't find any help on it, but since I didn't find anything anywhere I decided to at least put it somewhere to save someone else a little bit of time.

Problem

I've been using Moq for about a month now and I'm loving it. But it seems all of a sudden I can't use the Expect methods (Expect, ExpectGet, ExpectSet). If I open up a brand new Test project and reference Moq.dll (v 2.6.1014.1), then create a public interface like this:

public interface IMyInterface
{
    string MyMethod();
}

Then if I create a test method like this:

[TestMethod]
public void MyTest()
{
    Mock mock = new Mock<IMyTestMethod>();
}

The Expect methods are all missing from Intellisense. If I write it out anyway, it won't compile. I get the error:

'Moq.Mock' does not contain a definition for Expect() and no extension method for Expect() accepting a first argument of type 'Moq.Mock' could be found (are you missing a using directive or assembly reference?)

Solution

It turns out that most likely my problem was the cold medication I was on. But when you’re using Generics that Mock doesn’t equal Mock<IMyInterface>. So here’s what my test method should have been:

[TestMethod]
public void MyTest()
{
    Mock<IMyTestMethod> mock = new Mock<IMyTestMethod>();
}

It’s just a good thing for me that a laptop is not included in the “Heavy Machinery” warning on my cold medicine.

Labels: