public async Task GetRandomCatFactReturnsFactWithText() { var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock .Protected() // Setup the PROTECTED method to mock .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) // prepare the expected response of the mocked http call .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("{'Text':'This is a fact!'}"), }) .Verifiable(); var httpClient = new HttpClient(handlerMock.Object) { BaseAddress = new Uri("http://test.com/"), }; var unit = new CatFactApi(httpClient); var fact = await unit.GetRandomCatFact(); Assert.AreEqual("This is a fact!", fact.Successful.Item1); }
public ItemsViewModel(CatFactApi factApi) { _factApi = factApi; //Items = new RangeObservableCollection<CatFact>(); Items = new ObservableCollection <CatFact>(); LoadItemsCommand = new Command(async() => await ExecuteLoadItemsCommand()); ItemTapped = new Command <CatFact>(OnItemSelected); SelectionChanged = new Command(() => OnItemSelected(SelectedItem)); }
public MainPageViewModel(IHelloService helloService, CatFactApi catFactApi) { _helloService = helloService; _catFactApi = catFactApi; _text = _helloService.GetMessage(); ButtonAction = new Command(execute: async() => { IsBusy = true; var fact = await _catFactApi.GetRandomCatFact(); if (fact.Successful.success) { Text = fact.Successful.Value.Text; } else { var error = fact.Error.error; } IsBusy = false; }, canExecute: () => !IsBusy); }