Home > Net >  Unit testing how to Mock test object method
Unit testing how to Mock test object method

Time:09-27

There are three classes as follows:
 
Public interface IRepository1 {}
Public interface IService {
Public void once Function1 (List Ids);
Public void Function2 xxInfo (T)
}
Public class Service {
Public IRepository1 Repository1 {get; The set; }
Public void once Function1 (List Ids)
{
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:

reference 1st floor github_36000833 response:
Function2 is the realization of the Service, you want to test the Service class, should the Mock Function2 dependent function ,

Well, mostly, don't want to MockFunction2 dependence, because Function2, already has a corresponding unit tests,
I want to test the content of the once Function1, then passed to the Function2 ends, don't need to repeat the test Function2 content inside,
  •  Tags:  
  • C#
  • Related