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 <List <Country> > Get() { //Added non-blocking code for scalability return(await Task <List <Country> > .Run(() => { using (var op = new CountryOperations()) { return op.GetAll(); } })); }
public void Setup() { //Create Repo _fakeCountryRepo = new Mock <IGenericRepository <Int32, Country> >(); //Create unitOfWork and add Repo _fakeUnitOfWork = new Mock <IUnitOfWork>(); _fakeUnitOfWork.Setup(x => x.Repositories).Returns(new Dictionary <string, Object>()); _fakeUnitOfWork.Object.Repositories.Add(typeof(Country).Name, _fakeCountryRepo.Object); //Repositories mapped by entity names in unitOfWork(for detail check UnitOfWork.cs) //put mocked unitOfWork to the operation _countryOperations = new CountryOperations(_fakeUnitOfWork.Object); //get real countries 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 CountryOperations()) { this._realCountries = op.GetAll(); } _fakeCountryRepo.Setup(r => r.All().ToList()).Returns(this._realCountries); }