First some information about my development environment:
- .Net Framework 4.5
- Moq 4.10
- Autofac 4.2
- NUnit 3.11
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 ) );
}
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.
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 ) );
}