Public interface IRepository1 {}
Public interface IService {
Public void once Function1 (ListIds);
Public void Function2 xxInfo (T)
}
Public class Service {
Public IRepository1 Repository1 {get; The set; }
Public void once Function1 (ListIds)
{
Var list=this. Repository1. GetList (ids)//this can be a Mock
The foreach (var item in the list) {
Enclosing Function2 (item)//this should be how to simulate
}
}
}
Unit testing, I instantiate a Service, then the Mock a IRepository1, to forge the GetList, return the simulated data
However, cyclic invokes the Service. Function2 ()
How can I capture Function2 ()? Replace the running code inside?
What I use is behind Moq
//arrange
Service Service=new Service ();
Var mockRepository1=new MockMockRepository1. The Setup () Returns ();
Service. IRepository1=mockRepository1. Object ()
//act
Service. Once Function1 (new List{1, 2});
//assert
CodePudding user response:
Function2 is the realization of the Service, you want to test the Service class, should the Mock Function2 dependent function,CodePudding user response: