public async Task <List <HackerNewsPost> > GetPosts(int numberOfPosts) { List <HackerNewsPost> result = new List <HackerNewsPost>(); int currentPage = 1; do { //download page var pageData = await _hackerNewsServiceAgent.GetDataFromPage(currentPage ++).ConfigureAwait(false); if (_pageParser.IsPageValid(pageData)) { //parse and get all posts var posts = _pageParser.GetHackerNewsPosts(pageData); posts.ForEach(p => { //save only those that passes validation if (_hackerNewsPostItemValidator.IsValid(p)) { result.Add(p); } }); } } while (result.Count < numberOfPosts); //return only desired number of posts if (result.Count > numberOfPosts) { result = result.Take(numberOfPosts).ToList(); } return(result); }
public async Task Should() { int page = 1; string result = "<html></html>"; string expectedRequestString = $"news?p={page}"; var respone = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(result) }; _hackerNewsClient.Setup(c => c.GetRawPageData(It.Is <string>(p => p.Equals(expectedRequestString)))) .ReturnsAsync(respone); var html = await _hackerNewsServiceAgent.GetDataFromPage(page).ConfigureAwait(false);; Assert.Equal(result, html); }