Skip to content

Instantly share code, notes, and snippets.

View FraukeN's full-sized avatar
😺
Footloose and fancy free

Frauke Nonnenmacher FraukeN

😺
Footloose and fancy free
View GitHub Profile
@FraukeN
FraukeN / OverviewPage.cs
Created July 7, 2018 14:19
OverviewPage with DI v 1
public OverviewPage()
{
InitializeComponent();
IRepository<ToDoItem> repo = SimpleIoc.Default.GetInstance<IRepository<ToDoItem>>();
_viewModel = new OverviewVM(repo);
BindingContext = _viewModel;
}
@FraukeN
FraukeN / App.xaml.cs
Created July 7, 2018 14:16
Registering the repo interface against the MockRepo
public App()
{
InitializeComponent();
SimpleIoc.Default.Register<IRepository<ToDoItem>, MockRepository>();
MainPage = new OverviewPage();
}
@FraukeN
FraukeN / OverviewVMTests.cs
Last active June 15, 2018 22:49
OverviewVMTests with test for repo exception
[Test]
public async Task LoadData_sets_IsFaulted_to_true_if_LoadData_encounters_an_exception()
{
// Arrange
Mock<IRepository<ToDoItem>> repoMock = new Mock<IRepository<ToDoItem>>();
repoMock.Setup(repo => repo.GetAsync()).ThrowsAsync(new Exception());
OverviewVM vm = new OverviewVM(repoMock.Object);
// Act
@FraukeN
FraukeN / OverviewVM.cs
Last active June 15, 2018 22:44
OverviewVM new LoadData with exception handling
public OverviewVM(IRepository<ToDoItem> toDoRepo)
{
_toDoRepo = toDoRepo ?? throw new ArgumentNullException("toDoRepo");
IsFaulted = false;
ErrorMessage = string.Empty;
}
public async Task LoadData()
{
try
@FraukeN
FraukeN / OverviewVM.cs
Last active June 15, 2018 22:14
OverviewVM error handling
private bool _isFaulted;
public bool IsFaulted
{
get { return _isFaulted; }
set { Set(ref _isFaulted, value); }
}
private string _errorMessage;
public string ErrorMessage
{
@FraukeN
FraukeN / OverviewVMTests.cs
Created June 15, 2018 21:19
OverviewVM LoadData test with Moq
public async Task LoadData_loads_data_from_repository()
{
// Arrange
List<ToDoItem> toDoList = new List<ToDoItem>();
for (int i = 0; i < 5; i++)
{
toDoList.Add(new ToDoItem { Title = $"Item {i}" });
}
Mock<IRepository<ToDoItem>> repoMock = new Mock<IRepository<ToDoItem>>();
repoMock.Setup(repo => repo.GetAsync()).ReturnsAsync(toDoList);
@FraukeN
FraukeN / OverviewPage.cs
Created June 2, 2018 11:44
OverviewPage with DI
public OverviewPage()
{
InitializeComponent();
_viewModel = new OverviewVM(null);
BindingContext = _viewModel;
}
@FraukeN
FraukeN / OverviewVMTests.cs
Created June 2, 2018 11:38
OverviewVM null repository test
[Test]
public void creating_a_new_instance_of_OverviewVM_with_null_repository_throws_ArgumentNullException()
{
// Assert
Assert.That(() => new OverviewVM(null),
Throws.Exception
.TypeOf<ArgumentNullException>()
.With.Property("ParamName")
.EqualTo("toDoRepo"));
}
@FraukeN
FraukeN / OverviewVMTests.cs
Last active June 2, 2018 11:32
OverviewVM LoadData test with DI
public async Task LoadData_loads_data_from_repository()
{
// Arrange
OverviewVM vm = new OverviewVM(new MockRepository());
// Act
await vm.LoadData();
// Assert
Assert.That(vm.ToDoItems.Count(), Is.EqualTo(3));
@FraukeN
FraukeN / OverviewVM.cs
Created June 2, 2018 11:16
OverviewVM with repo injection
public OverviewVM(IRepository<ToDoItem> toDoRepo)
{
_toDoRepo = toDoRepo ?? throw new ArgumentNullException("toDoRepo");
}