Hi
I might be approaching this all wrong.But please help me to understand.
I really want to TDD building wpf application using Moq.
I would like to mock the viewmodel.
Application
Show a list of contacts and when you double click on a contact it shows the contact.
Test
Moq GetContactsCommand.Test it has been called.
Test that you get a list of contacts.
Not sure how to mock the viewModel and it's commands can you correct me?
So I have started to do the following
[Test]
public void Should_be_able_to_mock_getContactsCommand_and_get_a_list_of_contacts()
{
//Arrange
var expectedContacts = new ObservableCollection<ContactViewModel>
{
new ContactViewModel(new ContactModel
{
FirstName = "Jo",
LastName = "Bloggs",
Email = "
[email protected]"
}),
new ContactViewModel(new ContactModel
{
FirstName = "Mary",
LastName = "Bloggs",
Email = "
[email protected]"
})
};
var mock = new Mock<IContactListViewModel>();
mock.SetupGet(x => x.GetContactsCommand).Verifiable();
mock.SetupGet(x => x.Contacts).Returns(expectedContacts);
//Act
//?
//assert
mock.VerifySet(x => x.Contacts, Times.AtLeastOnce());
mock.Object.Contacts.Count.ShouldEqual(expectedContacts.Count);
}
public interface IContactListViewModel
{
ObservableCollection<ContactViewModel> Contacts { get; set; }
ICommand GetContactsCommand{ get; }
}
public interface IContactModel
{
string FirstName { get; set; }
string LastName { get; set; }
string Email { get; set; }
}
public class ContactModel : IContactModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
public class ContactViewModel : ViewModelBase
{
private readonly ContactModel _contactModel;
public ContactViewModel(ContactModel contactModel)
{
_contactModel = contactModel;
}
public string FirstName
{
get { return _contactModel.FirstName; }
set
{
_contactModel.FirstName = value;
OnPropertyChanged("FirstName");
}
}
public string LastName
{
get { return _contactModel.LastName; }
set
{
_contactModel.LastName = value;
OnPropertyChanged("LastName");
}
}
public string Email
{
get { return _contactModel.Emai; }
set
{
_contactModel.Email = value;
OnPropertyChanged("Email");
}
}
}
public class ContactListViewModel : ViewModelBase, IContactListViewModel
{
private ObservableCollection<ContactViewModel> _contacts;
public ObservableCollection<ContactViewModel> Contacts
{
get { return _contacts; }
set
{
_contacts = value;
OnPropertyChanged("Contacts");
}
}
private RelayCommand _getContactsCommand;
public ICommand GetContactsCommand
{
get
{
return _getContactsCommand ?? (_getContactsCommand = new RelayCommand(x => GetContacts(), x => CanGetContacts));
}
}
private static bool CanGetContacts
{
get { return true; }
}
private void GetContacts()
{
//pretend we are going to the service or db whatever
Contacts = new ObservableCollection<ContactViewModel>
{
new ContactViewModel(new ContactModel
{
FirstName = "Jo",
LastName = "Bloggs",
Email = "
[email protected]"
}),
new ContactViewModel(new ContactModel
{
FirstName = "Mary",
LastName = "Bloggs",
Email = "
[email protected]"
})
};
}
}