i am new to unittesting with rhino mocks and i am confused how to mock my dependency which are initialized in controller constructor using unity container.
as per the below code i have to unittest "GetHelloWorld" method with two dependency TestRepository and RedisCacheManager .
public class TestController : BaseController
{
private ITestRepository testRepo;
private IRedisCacheManager cacheManager;
public TestController()
{
testRepo = UnityResolver.Resolve<ITestRepository>();
cacheManager = UnityResolver.Resolve<IRedisCacheManager>();
}
public ActionResult GetHelloWorld()
{
LoggerHelper.WriteInfo("Inside GetHelloWorld Method..");
testRepo.PutDataInCustomAzureDB();
cacheManager.Add("Test", "TestData", new TimeSpan(0, 1, 0));
var getData = cacheManager.Get<string>("Test");
cacheManager.Add("Test", "TestData123", new TimeSpan(0, 1, 0));
var getDataa = cacheManager.Get<string>("Test");
LoggerHelper.WriteInfo("Leaving GetHelloWorld Method..");
return null;
}
}
}
You have to have a separate UnityResolver
for the unit test project and there you have to send mock objects in the Resolve
method. That is have a separate container
for the unit test project and then
Mock<IRedisCacheManager> _mockRedis = new Mock<IRedisCacheManager>();
container.RegisterInstance <IRedisCacheManager>(_mockRedis.Object);
Another way of doing this would be to inject the resolver
or the dependencies in the class itself
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.