dimanche 10 juillet 2016

Unit test a method reading file

I search on net but couldnt find proper solution. My issue is I want to test my readFileToList method which is make a filter in the file(csv) and; create List of objects. Its okay in my application I try to test this with following method in unit test project:

        [TestMethod]
        public void readingFile()
        {
           HomeController controller = new HomeController();
           List<City> result = controller.readFileToList("sample_data.csv", Pairing.Of("cityname", "Antalya"));
           Assert.AreEqual(83, result.Count);//"cityname" equals to Antalya is 83 
        }

I debug this test and see the error in of the function; "HttpRuntime.AppDomainAppPath" return null, and I wonder how can I handle this and next coming issues for testing this method. I just want this test check count of generated file equals to expected count(83)

function:

public List<City> readFileToList(string filename, params KeyValuePair<string, object>[] queryparams)
        {
            string fullName = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data", filename);
            string[] lines = System.IO.File.ReadAllLines(fullName);
            IQueryable<City> cities = lines.Skip(1).Select(l => new City
            {
                name = l.Split(',')[0],
                cityCode = Convert.ToInt32(l.Split(',')[1]),
                district = l.Split(',')[2],
                zipCode = l.Split(',')[3],
            }).AsQueryable();
            foreach (KeyValuePair<string, object> item in queryparams)
            {
                if (item.Key == "cityname" && item.Value.ToString() != "0")
                    cities = cities.Where(w => w.cityCode.ToString() == item.Value.ToString());               
            }
               return cities.ToList();
        }

Aucun commentaire:

Enregistrer un commentaire