简体   繁体   中英

Setting up a Moq Mock<Interface> call to a method that returns a string

I've got a class and a method that looks something like this:

public class FieldLookup : IFieldLookup
{
    public string LookupField(string Var1, string Var2)
    {
        // do a database lookup
    }
}

This works fine in the actual class where it's being used with Dependency Injection. What I'm trying to figure out is how, in the Unit Test, do I use the Moq framework to mock a call to this method.

var fieldLookup = new Mock<IFieldLookup>();

Then, how do I do this....?

fieldLookup.Setup<string>(x => x.LookupField("", "").Returns("something"));
fieldLookup.Setup(x => x.LookupField("", "").Returns("something"));

Both of these tell me that "String does not contain a definition for Return". Been playing around, but not getting it.

You put parentheses on the wrong place, you need to call the Returns method on the object returned by the Setup method.

fieldLookup.Setup<string>(x => x.LookupField("", "")).Returns("something");
fieldLookup.Setup(x => x.LookupField("", "")).Returns("something");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM