mardi 1 mars 2016

Unit test for my ViewModel?

I'm new to unit testing and I'm really stuck atm so I could really use some help.

Some application info
I have a WPF application in MVVM. It gets data from a database (classes generated via .edmx).
All linq queries are handled by methods in the Database class. In CustomerListViewModel, it makes a list of all Customers to be shown in the CustomerListView.

My problem
I am new to Unit Testing. I've read about it and tried to make it work. But as I understand, it should/can be done without touching the DB. I tried to find as much info as I could, but it wouldn't work with what I have. And now I'm basically stuck.

My question How do I unit test this piece of code? How can I know if I've successfully queried the database (with or without touching the DB in the unit test)?
(If I understand it for this piece, I can figure the rest of the classes and methods out on my own)

The code
CustomerListViewModel:

public CustomerListViewModel()
{
    MyObservableCollection<Customer> listCustomers = new MyObservableCollection<Customer>();
    ListCustomers = App.Database.GetCustomerList();
}

private void GetListCustomers()
{
    ListCustomers = App.Database.GetCustomerList();
    if (App.Database.hasError)
        App.Messenger.NotifyColleagues("SetStatus", App.Database.errorMessage);
}

Database:

public MyObservableCollection<Customer> GetCustomerList()
{
    hasError = false;
    MyObservableCollection<Customer> customerList = new MyObservableCollection<Customer>();
    try
    {
        QRM_Entities dc = new QRM_Entities();
        var query =
            from customers in dc.Customer
            select customers;

        foreach (Customer cust in query)
        {
            customerList.Add(cust);
        }
    }
    catch (Exception ex)
    {
        errorMessage = "GetCustomerList() error, " + ex.Message;
        hasError = true;
    }
    return customerList;
}

Aucun commentaire:

Enregistrer un commentaire