传递 Moqs It.IsAny<string>作为方法参数 [英] Passing Moqs It.IsAny&lt;string&gt; as method argument

查看:51
本文介绍了传递 Moqs It.IsAny<string>作为方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是关于我的开发环境的一些信息:

First some information about my development environment:

  • .Net 框架 4.5
  • 最小起订量 4.10
  • Autofac 4.2
  • NUnit 3.11

我尝试模拟一个接受一些字符串参数的函数,我想使用 It.IsAny() 来设置.通常我会这样:

I try to mock a function that takes some string arguments and I would like to use It.IsAny<string>() to setup. Normally I would to that like this:

using ( AutoMock mock = AutoMock.GetLoose() ) {
    mock.Mock<SomeInterface>()
                .Setup( x => x.someFunction(
                    It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<string>() ) );
//...
}

但现在我想调用一个进行设置的功能,所以我不必复制粘贴上面的代码并使我的单元测试更好看".我想像这样:

But now I would like to call a fucntion that makes the setup, so I do not have to copy paste the code above and make my unit tests a little bit "better looking". I imagine something like this:

using ( AutoMock mock = AutoMock.GetLoose() ) {
    UnknownType anyString = It.IsAny<string>();
    setup( mock, anyString );
//...
}

void setup( Automock mock, UnknownType anyString ){
    mock.Mock<SomeInterface>()
            .Setup( x => x.someFunction( anyString, anyString, anyString ) );     
}

有人知道解决方案吗?当我使用 string 甚至 var 作为未知类型时,变量 anyString 之后保存值 nullUnknownType anyString = It.IsAny();.预先感谢您的回答.

Does someone know a solution for that? I when I use string or even var as Unknown type the variable anyString hold the value null after UnknownType anyString = It.IsAny<string>();. Thanks in advance for your answers.

进一步说明:

我需要为每个参数指定不同的值.所以它看起来像这样:

I need to specify different values for every argument. So It could look like this:

using ( AutoMock mock = AutoMock.GetLoose() ) {
   UnknownType string1 = It.IsAny<string>;
   UnknownType string2 = It.Is<string>( s => s.Equals( "Specific string" )  );
   UnknownType string3 = It.IsAny<string>;
   setup( mock, string1, string2, string3 );
//...           
}

private static void setup( AutoMock mock, 
   UnknownType string1, UnknownType string2, UnknownType string3 ) {
   mock.Mock<SomeInterface>().Setup( x => x.someFunction( string1, string2, string3 ) );
}

推荐答案

It.* 旨在用作 Setup 表达式参数,而不是作为参数传递/变量,默认情况下,它们返回 null.

It.* is meant to be used as Setup expression arguments and not passed around as parameters/variables as, by default, they return null.

据我所知,您所要求的似乎不可能.

To the best of my knowledge, what you are requesting does not appear to be possible.

通过使用泛型我能想到的最接近的事情是创建一个扩展方法来作用于使用所需表达式进行模拟的自动模拟

The closest thing I can think of through the use of generics is to create an extension method to act on the auto mock that takes the desired expression to mock

public static class AutomockExtension {
    public static Moq.Language.Flow.ISetup<T> Setup<T>(this Automock mock, Expression<Action<T>> expression) where T : class {
        return mock.Mock<T>().Setup(expression);
    }

    public static Moq.Language.Flow.ISetup<T, TValue> Setup<T, TValue>(this Automock mock, Expression<Func<T, TValue>> expression) where T : class {
        return mock.Mock<T>().Setup(expression);
    }
}

这实际上并没有为您节省多少术语或重复代码,因为您仍然必须在表达式中调用 It.*

which really does not save you much in terms or repeated code as you will still have to invoke It.* in the expression

using ( AutoMock mock = AutoMock.GetLoose() ) {

    mock.Setup<SomeInterface>(_ => 
        _.someFunction(
            It.IsAny<string>(),
            It.Is<string>( s => s.Equals( "Specific string" ),
            It.IsAny<string>()
        ) 
    );

    //...           
}

<小时>

原答案

使用泛型

void setup<T>(Automock mock){
    mock.Mock<SomeInterface>()
            .Setup(_ => _.someFunction(
                It.IsAny<T>(), 
                It.IsAny<T>(), 
                It.IsAny<T>()));     
}

然后让您根据需要调用设置

which would then let you invoke the setup as needed like

using ( AutoMock mock = AutoMock.GetLoose() ) {
    setup<string>(mock);
    //...
}

这篇关于传递 Moqs It.IsAny<string>作为方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆