kick it on DotNetKicks.com   Shout it  

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<IMyInterface>();
}

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<IMyInterface> mock = new Mock<IMyInterface>();
}

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


Feedback

# 

Gravatar One very good reason to use "var" instead of explicitly specifying the type *twice* ;) 3/11/2009 10:53 AM | noreply@blogger.com (kzu)

# 

Gravatar Yeah, I have a colleague who's not gonna let me live that one down. I guess I'll be using var a bit more now to prevent little mishaps like that one. 3/11/2009 11:09 AM | noreply@blogger.com (Mark J. Miller)

# 

Gravatar That's right, Mark - var is your friend and you should learn to use it more often! Obviously it isn't the golden hammer, so you have to be careful to not use it in places that don't make sense, but that's more the exception than the rule.

var will lead you to the promised land. :P 3/11/2009 12:28 PM | noreply@blogger.com (JasonBunting)

# 

Gravatar And so it starts... 3/11/2009 12:36 PM | noreply@blogger.com (Mark J. Miller)

# 

Gravatar I've noticed that some people are getting carried away with var and have completely abandoned using specific typing. If that is how its going to be, then lets propose that C# lose the var keyword altogether and just automatically assume we mean var without the use of the keyword. So instead of var foo = 12; just foo = 12; and if it is the first instance of foo, then it knows to create it as a variable of type ... int....yeah...thats a good idea. 4/1/2009 3:57 PM | noreply@blogger.com (JasonBunting)

# 

Gravatar Nice, I like how Tony came in and posted using my name. Thanks Tony, I appreciate that. 4/16/2009 1:01 PM | noreply@blogger.com (JasonBunting)

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

Gravatar Should that not be Mock<IIMyInterface>? 11/2/2009 8:22 PM | Dean Conway

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

Gravatar Doh! Yes, I think you're right. How'd I miss that? 11/3/2009 7:09 AM | MarkJMiller

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

Gravatar Expect is obsolete. Use Setup instead. 3/29/2010 10:06 AM | Piotr

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

Gravatar @Piotr, thanks. This is an old post (see version number referenced in the post), which is why it's obsolete. But thanks for your comments. 3/30/2010 4:09 PM | MarkJMiller

Post a comment





 

Please add 6 and 7 and type the answer here:

 

 

Copyright © Mark J. Miller