public void Setup() { //Create Repo _fakeSmsRepo = new Mock <IGenericRepository <Int32, Sms> >(); //Create unitOfWork and add Repo _fakeUnitOfWork = new Mock <IUnitOfWork>(); _fakeUnitOfWork.Setup(x => x.Repositories).Returns(new Dictionary <string, Object>()); _fakeUnitOfWork.Object.Repositories.Add(typeof(Sms).Name, _fakeSmsRepo.Object); //Repositories mapped by entity names in unitOfWork(for detail check UnitOfWork.cs) _fakeUnitOfWork.Object.Repositories.Add(typeof(Country).Name, _fakeCountryRepo.Object); //Repositories mapped by entity names in unitOfWork(for detail check UnitOfWork.cs) //Will work for first line of SendSms List <Country> countries; using (var op = new CountryOperations()) { countries = op.GetAll(); _fakeCountryRepo.Setup(r => r.FindBy(x => x.Cc == toParameter.Substring(1, 2))).Returns(countries.Find(x => x.Cc == toParameter)); } //get real smses from db and add it to repo memory //unit tests should not go to database, all logic should be in memory. using (var op = new SmsOperations()) { _realGetSentSmsResult = op.GetSentSms(dateFrom, dateTo, skip, take); _fakeSmsRepo.Setup(r => r.FilterBy(x => x.DateTime >= dateFrom && x.DateTime <= dateTo).ToList()) .Returns(op.GetAll().FindAll(x => x.DateTime >= dateFrom && x.DateTime <= dateTo).ToList()); } //put mocked unitOfWork to the operation _smsOperations = new SmsOperations(_fakeUnitOfWork.Object); }
public async Task <SentSms> GetSentSms(DateTime dateTimeFrom, DateTime dateTimeTo, int skip, int take) { //Added non-blocking code for scalability return(await Task <SentSms> .Run(() => { using (var op = new SmsOperations()) { return op.GetSentSms(dateTimeFrom, dateTimeTo, skip, take); } })); }