简体   繁体   中英

How to Moq a method in C# whose input parameter is of type Enum

How to Moq a method in C# whose input parameter is of type Enum?

 Moq.MockRepository mock = new Moq.MockRepository(MockBehaviour.Loose);
    var myInstance =mock.Create<IMyMethod>();
    myInstance.SetUp(x=>x.myMethod(It.IsAny<**`EnumType`**>()).Verifiable();

How to Give ENUMType here

I believe you need to do the following.

mock.Setup(s => s.YourMethod(It.IsAny<YourEnumType>())).Verifiable();

Obviously, remember to put the using statements linking this file to wherever you have your enum type saved.

Also as an extra note. To make this dynamic (So you don't just take any, you actually use the value coming in to decide what your mock returns) you can do the following

 mock.Setup(s => s.YourMethod(It.IsAny<YourEnumType>()))
 .ReturnsAsync((YourEnymType enumType)
 => yourMock.Where(x => x.EnumValue == enumType).SingleOrDefault());

Notice This is done for async, if you're not doing an asynchronous, switch the "ReturnsAsync" to a "Returns"

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