lundi 4 mai 2015

Unit Test for Task method with dependency injection

I am new to writing Unit Test in visual studio. In my web application i have following contents.

1> Interface

public interface IGettProxy
{
    Task<List<CityDetails>> getCity();
    Task<List<CountryDetails>> getCountry(int cityId);
}

2> Contracts (Implementation of Interface)

  public async Task<List<CityDetails>> getCity()
    {
        try
        {

            _serviceUrl = String.Format("{0}/Search/getCityinfo", _serviceUrl);
            string requestUri = _serviceUrl;
            client = new HttpClient();
            var response = await client.GetAsync(requestUri);
            if (response.IsSuccessStatusCode)
            {
                string json = await response.Content.ReadAsStringAsync();
                var Result = new          JavaScriptSerializer().Deserialize<List<CityDetails>>(json);
                return Result;
            }
            else
            {
                throw new Exception("Errorhandling message");
            }
        }
        catch (Exception ex) { throw ex; }
    }


    public async Task<List<CountryDetails>> getCountry(int cityId)
    {
        try
        {
            _serviceUrl = String.Format("{0}/Search/getcountryinfo?cityId={1}", _serviceUrl, cityId);
            string requestUri = _serviceUrl;
            client = new HttpClient();
            var response = await client.GetAsync(requestUri);
            if (response.IsSuccessStatusCode)
            {
                string json = await response.Content.ReadAsStringAsync();
                var Result = new JavaScriptSerializer().Deserialize<List<CountryDetails>>(json);
                return Result;
            }
            else
            {
                throw new Exception("Errorhandling message");
            }
        }
        catch (Exception ex) { throw ex; }
    }

3> Controller

       private IGettProxy igettProxy;

    public GettController(IGettProxy gettProxy)
    {
        igettProxy = gettProxy;
    }

    /// <summary>
    /// Invoked on Page Load
    /// </summary>
    /// <returns></returns>
    public async Task<ActionResult> Getdet()
    { 
        try
        {
            List<CityDetails> cityDetails = await igettProxy.getCity();
            SearchModel viewModel = new SearchModel();
            viewModel.cityDetail = cityDetails;
            return View(viewModel);
        }
        catch (Exception ex) { throw ex; }
    }

    /// <summary>
    /// Get Country list based on city information
    /// </summary>
    /// <param name="cityId"></param>
    /// <returns></returns>
    public async Task<JsonResult> getCountry (int cityId)
    {
        try
        {
            List<CountryDetails> CountryDetails = await iSearchProxy.getCountry(cityId);
            return Json(CountryDetails,JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex) { throw ex; }
    }

I have different class libraries for data member.

For injection configuration i am using Unity method.

So in this view i have drop down to bind city, country values.

For this drop down binding i want to write unit test. Please help me with this detail. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire