简体   繁体   中英

MicroSoft Fakes on static with different returns

I'm trying to unit test an object that uses a generic static factory class. I'm not really at liberty to write out the factory.

I have to use Microsoft Fakes to shim it. (I think) Never used it before. My question is that it is a generic factory method used three times and needs to return 3 different sets of results. Mock has the ability to seqence a method and return different results each time. Does Fakes have this ability?

public static ReadOnlyCollection<T> Build<T>(IObjectA objA, IObjectB objB) where T : class

Inside the object it calls this method 3 times with different interfaces

IEnumerable<Base1> list = Factory.Build<Base1>(objA, objB);
IEnumerable<Interface1> list= Factory.Build<Interface1>(objA, objB);
IEnumerable<Interface2> list= Factory.Build<Interface2>(objA, objB);

How would the test method look for something like that?

using(ShimsContext.Create()) {


Mock<Base1> mockObj1 = new Mock<Base1>();
                //manager.Setup(t=> t.)

Mock<Interface1> mockObj2= new Mock<Interface1>();
                //manager.Setup(t=> t.)

Fakes.ShimFactory.BuildOf1IObjectAIObjectB<Base1>((objA, objB) => new List<Base1>() { mockObj1.Object }.AsReadOnly());

Fakes.ShimFactory.BuildOf1IObjectAIObjectB<Interface1>((objA, objB) => new          
 List<Interface1>() { mockobj2.Object }.AsReadOnly());

//Uses factory method 3 times
MyObjectThatUsesTheFactory = new MyObjectThatUsesTheFactory();
//test
}

Thanks for any pointers!

Apparently I was on track. It returns different results as laid out above. Just a matter of getting all the mocks correct.

Thanks

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