mardi 6 octobre 2015

ASP MVC Unit testing a paged list

Say I have a controller which looks something like the one below where I fetch some items out of a database and add them to a paged list. How would I go about writing a unit test for this? I can't seem to find any good material on this.

Controller:

public class ErrorController : Controller
{
    public ErrorModel Errors { get; set; }
    public List<ErrorModel> ErrorList { get; set; }

    public ActionResult Error(int? page)
    {
        string cs = "Data Source=" + "some\\path";

        using (SQLiteConnection con = new SQLiteConnection(cs))
        {
            var listOfErrors = new List<ErrorModel>();
            string stm = "SELECT * FROM Error WHERE Checked == 'False'";
            con.Open();

            using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
            {
                using (SQLiteDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        listOfErrors.Add(new ErrorModel
                        {
                            Id = rdr["ID"].ToString(),
                            Type = rdr["Type"].ToString(),
                        });
                    }

                    rdr.Close();
                    ErrorList = listOfErrors;
                }
            }

            con.Close();
        }

        // stuff for paging
        int pageSize = 10;
        int pageNumber = (page ?? 1); // if there is no page, return page 1

        return View(ErrorList.ToPagedList(pageNumber, pageSize));
    }

My current, obviously not adequate unit test:

[TestClass]
public class ErrorControllerTest
{
    [TestMethod]
    public void TestErrorView()
    {
        var controller = new ErrorController();
        var result = controller.Error(1) as ViewResult;
        Assert.AreEqual("Error", result.ViewName);
    }
}

Any hints greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire