具有依赖注入的Task方法的单元测试

时间:2015-05-04 09:23:07

标签: c# unit-testing model-view-controller dependency-injection microsoft-fakes

我是视觉工作室中编写单元测试的新手。在我的网络应用程序中,我有以下内容。

1>接口

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

2 - ;合同(接口的实现)

  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&GT;控制器

       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; }
    }

我有不同的数据成员类库。

对于注射配置,我使用的是Unity方法。

所以在这个视图中,我已经下降到绑定城市,国家价值观。

对于这个下拉式绑定,我想编写单元测试。请帮我详细说明。提前谢谢。

我的测试方法

 [TestMethod]
        public void bookAppointment()
        {

             List<CityDetails> cityDetails = new List<CityDetails>();
             cityDetails.Add(new CityDetails {ID=1,CityName="Delhi"});
          //  var mockproxy = new StubISearchProxy();
            StubISearchProxy searchProxy = new StubISearchProxy();

            searchProxy.GetCity = () =>  cityDetails;

            SearchController searchController = new SearchController(searchProxy);
            var str = searchController.getCity();
        }

1 个答案:

答案 0 :(得分:0)

在DI Unity中将为您解决此接口实现。为了测试这个,你必须创建一个实现你的接口的假类,并注入(在构造函数上)。 类似的东西:

C:\Users\XXXX>javac -version
javac 1.8.0_31

C:\Users\XXXX>javac Main.java

C:\Users\XXXX>java Main
Exception in thread "main" java.lang.NullPointerException
        at Main.method(Main.java:15)
        at Main.execute(Main.java:10)
        at Main.main(Main.java:5)

然后当你实例化你的控制器时,你将传递这个虚假的接口实现(构造函数需要的那个)。

现在你可以测试一下。