[英]Passing Moqs It.IsAny<string> as method argument
First some information about my development environment: 首先介绍一下我的开发环境:
I try to mock a function that takes some string arguments and I would like to use It.IsAny<string>()
to setup. 我尝试模拟一个带有一些字符串参数的函数,我想使用
It.IsAny<string>()
来设置。 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 ) );
}
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>();
当我使用
string
或甚至var
作为Unknown类型时,变量anyString
在UnknownType anyString = It.IsAny<string>();
之后保持值为null
UnknownType anyString = It.IsAny<string>();
. 。 Thanks in advance for your answers.
提前感谢您的回答。
Further description: 进一步说明:
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.*
is meant to be used as Setup
expression arguments and not passed around as parameters/variables as, by default, they return null . It.*
用作Setup
表达式参数,不作为参数/变量传递,因为默认情况下它们返回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);
}
}
which really does not save you much in terms or repeated code as you will still have to invoke It.*
in the expression 这真的不会在术语或重复代码中保存很多,因为你仍然需要在表达式中调用
It.*
using ( AutoMock mock = AutoMock.GetLoose() ) {
mock.Setup<SomeInterface>(_ =>
_.someFunction(
It.IsAny<string>(),
It.Is<string>( s => s.Equals( "Specific string" ),
It.IsAny<string>()
)
);
//...
}
Original Answer 原始答案
Use generics 使用泛型
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);
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.