lundi 30 novembre 2015

Write test method for MVVM- Ninject DI - Entity Framework

I'm learning to write test method. But I still don't know how to created fake data for the test class.

Here's my code:

public interface IData
{
    Staff GetUser(string username, string password);
}
// Implement class , which get data from a DataContext
public class EFData: IData
{
    HotelDataContext dataContext;

    public Staff GetUser(string username, string password)
    {
        var staff = dataContext.Staffs.Where(s => s.Username == username && s.Password == password).FirstOrDefault();
        return staff;
    }
}

// My ViewModel which I want to test
public class StaffViewModel: INotifyPropertyChanged
{
    IData _data;

    // I use Ninject for Dependency Injection here
    public StaffViewModel(IData data)
    {
        _data = data;
    }

    // I want to test this method
    public bool Login(string username, string password)
    {
        var staff = _data.GetUser(username, password);
        return (null != staff);
    }
}

As I know, now I need to write some class like this , but I have no idea how to create the test data for this method.

[TestClass]
public class StaffVMTest
{
    [TestMethod]
    public void TestLogin()
    {
        ////
        //   wrong_test_username, pw ; success_test_username, pw   = ????
        ////

        var testResult1 = vm.Login(wrong_test_username, wrong_test_pw);
        var testResult2 = vm.Login(success_test_username, success_test_pw);

        Assert.AreEqual(false, testResult1);            
        Assert.AreEqual(true, testResult2);
    }
}

Aucun commentaire:

Enregistrer un commentaire