简体   繁体   English

设置最小起订量 <Interface> 调用返回字符串的方法

[英]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. 我要弄清楚的是,在单元测试中,如何使用Moq框架来模拟对此方法的调用。

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". 这两个都告诉我“字符串不包含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. 您将括号放在错误的位置,需要在Setup方法返回的对象上调用Returns方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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