Browse Source

Merge pull request #176 from dotnet-architecture/xamarin

Unit tests
pull/223/head
David Britch 7 years ago
committed by GitHub
parent
commit
a0d8f93458
25 changed files with 701 additions and 94 deletions
  1. +1
    -1
      src/Mobile/eShopOnContainers/eShopOnContainers.Core/Behaviors/EventToCommandBehavior.cs
  2. +5
    -7
      src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Basket/BasketService.cs
  3. +0
    -7
      src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs
  4. +11
    -24
      src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogService.cs
  5. +0
    -1
      src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/ICatalogService.cs
  6. +0
    -2
      src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/CatalogViewModel.cs
  7. +1
    -1
      src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.Droid/MainActivity.cs
  8. +1
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.Droid/eShopOnContainers.TestRunner.Droid.csproj
  9. +1
    -1
      src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.Windows/App.xaml.cs
  10. +1
    -1
      src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.iOS/AppDelegate.cs
  11. +6
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.iOS/eShopOnContainers.TestRunner.iOS.csproj
  12. +1
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.iOS/packages.config
  13. +120
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Behaviors/EventToCommandBehaviorTests.cs
  14. +0
    -27
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/DummyTests.cs
  15. +12
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Mocks/MockEventToCommandBehavior.cs
  16. +53
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Mocks/MockViewModel.cs
  17. +0
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Services/BasketServiceTests.cs
  18. +0
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Services/CatalogServiceTests.cs
  19. +9
    -11
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Services/OrdersServiceTests.cs
  20. +223
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/ViewModels/CatalogViewModelTests.cs
  21. +54
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/ViewModels/MainViewModelTests.cs
  22. +113
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/ViewModels/MockViewModelTests.cs
  23. +55
    -0
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/ViewModels/OrderViewModelTests.cs
  24. +26
    -4
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/eShopOnContainers.UnitTests.csproj
  25. +8
    -7
      src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/packages.config

+ 1
- 1
src/Mobile/eShopOnContainers/eShopOnContainers.Core/Behaviors/EventToCommandBehavior.cs View File

@ -31,7 +31,7 @@ namespace eShopOnContainers.Core.Behaviors
BindableProperty.CreateAttached("EventArgsConverterParameter", typeof(object), typeof(EventToCommandBehavior), null,
BindingMode.OneWay);
private Delegate _handler;
protected Delegate _handler;
private EventInfo _eventInfo;
public string EventName


+ 5
- 7
src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Basket/BasketService.cs View File

@ -16,8 +16,7 @@ namespace eShopOnContainers.Core.Services.Basket
}
public async Task<CustomerBasket> GetBasketAsync(string guidUser, string token)
{
{
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.BasketEndpoint);
builder.Path = guidUser;
@ -30,18 +29,17 @@ namespace eShopOnContainers.Core.Services.Basket
ServicesHelper.FixBasketItemPictureUri(basket?.Items);
return basket;
}
public async Task<CustomerBasket> UpdateBasketAsync(CustomerBasket customerBasket, string token)
{
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.BasketEndpoint);
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.BasketEndpoint);
string uri = builder.ToString();
string uri = builder.ToString();
var result = await _requestProvider.PostAsync(uri, customerBasket, token);
var result = await _requestProvider.PostAsync(uri, customerBasket, token);
return result;
return result;
}
public async Task ClearBasketAsync(string guidUser, string token)


+ 0
- 7
src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs View File

@ -61,12 +61,5 @@ namespace eShopOnContainers.Core.Services.Catalog
return MockCatalogType;
}
public async Task<CatalogItem> GetCatalogItemAsync(string id)
{
await Task.Delay(500);
return MockCatalog.FirstOrDefault(c => c.Id == id);
}
}
}

+ 11
- 24
src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogService.cs View File

@ -20,7 +20,6 @@ namespace eShopOnContainers.Core.Services.Catalog
public async Task<ObservableCollection<CatalogItem>> FilterAsync(int catalogBrandId, int catalogTypeId)
{
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.CatalogEndpoint);
builder.Path = string.Format("api/v1/catalog/items/type/{0}/brand/{1}", catalogTypeId, catalogBrandId);
@ -34,12 +33,10 @@ namespace eShopOnContainers.Core.Services.Catalog
return catalog?.Data.ToObservableCollection();
else
return new ObservableCollection<CatalogItem>();
}
public async Task<ObservableCollection<CatalogItem>> GetCatalogAsync()
{
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.CatalogEndpoint);
builder.Path = "api/v1/catalog/items";
@ -56,37 +53,28 @@ namespace eShopOnContainers.Core.Services.Catalog
return catalog?.Data.ToObservableCollection();
}
else
return new ObservableCollection<CatalogItem>();
}
public Task<CatalogItem> GetCatalogItemAsync(string id)
{
throw new NotImplementedException();
return new ObservableCollection<CatalogItem>();
}
public async Task<ObservableCollection<CatalogBrand>> GetCatalogBrandAsync()
{
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.CatalogEndpoint);
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.CatalogEndpoint);
builder.Path = "api/v1/catalog/catalogbrands";
string uri = builder.ToString();
builder.Path = "api/v1/catalog/catalogbrands";
IEnumerable<CatalogBrand> brands =
await _requestProvider.GetAsync<IEnumerable<CatalogBrand>>(uri);
string uri = builder.ToString();
if (brands != null)
return brands?.ToObservableCollection();
else
return new ObservableCollection<CatalogBrand>();
IEnumerable<CatalogBrand> brands =
await _requestProvider.GetAsync<IEnumerable<CatalogBrand>>(uri);
if (brands != null)
return brands?.ToObservableCollection();
else
return new ObservableCollection<CatalogBrand>();
}
public async Task<ObservableCollection<CatalogType>> GetCatalogTypeAsync()
{
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.CatalogEndpoint);
builder.Path = "api/v1/catalog/catalogtypes";
@ -99,8 +87,7 @@ namespace eShopOnContainers.Core.Services.Catalog
if (types != null)
return types.ToObservableCollection();
else
return new ObservableCollection<CatalogType>();
return new ObservableCollection<CatalogType>();
}
}
}

+ 0
- 1
src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/ICatalogService.cs View File

@ -10,6 +10,5 @@ namespace eShopOnContainers.Core.Services.Catalog
Task<ObservableCollection<CatalogItem>> FilterAsync(int catalogBrandId, int catalogTypeId);
Task<ObservableCollection<CatalogType>> GetCatalogTypeAsync();
Task<ObservableCollection<CatalogItem>> GetCatalogAsync();
Task<CatalogItem> GetCatalogItemAsync(string id);
}
}

+ 0
- 2
src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/CatalogViewModel.cs View File

@ -5,8 +5,6 @@ using Xamarin.Forms;
using eShopOnContainers.Core.Models.Catalog;
using eShopOnContainers.Core.Services.Catalog;
using System.Windows.Input;
using eShopOnContainers.Core.Services.Basket;
using eShopOnContainers.Core.Services.User;
namespace eShopOnContainers.Core.ViewModels
{


+ 1
- 1
src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.Droid/MainActivity.cs View File

@ -14,7 +14,7 @@ namespace eShopOnContainers.TestRunner.Droid
AddExecutionAssembly(typeof(ExtensibilityPointFactory).Assembly);
// or in any reference assemblies getting the Assembly from any type/class
AddTestAssembly(typeof(UnitTests.DummyTests).Assembly);
AddTestAssembly(typeof(UnitTests.CatalogViewModelTests).Assembly);
base.OnCreate(bundle);
}


+ 1
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.Droid/eShopOnContainers.TestRunner.Droid.csproj View File

@ -20,6 +20,7 @@
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<AndroidTlsProvider></AndroidTlsProvider>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>


+ 1
- 1
src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.Windows/App.xaml.cs View File

@ -12,7 +12,7 @@ namespace eShopOnContainers.TestRunner.Windows
{
// Otherwise you need to ensure that the test assemblies will
// become part of the app bundle
AddTestAssembly(typeof(UnitTests.DummyTests).GetTypeInfo().Assembly);
AddTestAssembly(typeof(UnitTests.CatalogViewModelTests).GetTypeInfo().Assembly);
}
}
}

+ 1
- 1
src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.iOS/AppDelegate.cs View File

@ -17,7 +17,7 @@ namespace eShopOnContainers.TestRunner.iOS
// Otherwise you need to ensure that the test assemblies will
// become part of the app bundle
AddTestAssembly(typeof(UnitTests.DummyTests).Assembly);
AddTestAssembly(typeof(UnitTests.CatalogViewModelTests).Assembly);
return base.FinishedLaunching(app, options);
}


+ 6
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.iOS/eShopOnContainers.TestRunner.iOS.csproj View File

@ -155,6 +155,12 @@
<HintPath>..\..\..\..\packages\xunit.runner.utility.2.2.0-beta4-build3444\lib\netstandard1.1\xunit.runner.utility.dotnet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Plugin.Settings.Abstractions">
<HintPath>..\..\..\..\packages\Xam.Plugins.Settings.2.6.0.12-beta\lib\Xamarin.iOS10\Plugin.Settings.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Plugin.Settings">
<HintPath>..\..\..\..\packages\Xam.Plugins.Settings.2.6.0.12-beta\lib\Xamarin.iOS10\Plugin.Settings.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="AppDelegate.cs.txt" />


+ 1
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.iOS/packages.config View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommonServiceLocator" version="1.3" targetFramework="xamarinios10" />
<package id="Xam.Plugins.Settings" version="2.6.0.12-beta" targetFramework="xamarinios10" />
<package id="Xamarin.Forms" version="2.3.3.175" targetFramework="xamarinios10" />
<package id="xunit" version="2.2.0-beta4-build3444" targetFramework="xamarinios10" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="xamarinios10" />


+ 120
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Behaviors/EventToCommandBehaviorTests.cs View File

@ -0,0 +1,120 @@
using Xunit;
using Xamarin.Forms;
using System;
using System.Globalization;
namespace eShopOnContainers.UnitTests
{
public class EventToCommandBehaviorTests
{
[Fact]
public void InvalidEventNameShouldThrowArgumentExceptionText()
{
var behavior = new MockEventToCommandBehavior
{
EventName = "OnItemTapped"
};
var listView = new ListView();
Assert.Throws<ArgumentException>(() => listView.Behaviors.Add(behavior));
}
[Fact]
public void CommandExecutedWhenEventFiresText()
{
bool executedCommand = false;
var behavior = new MockEventToCommandBehavior
{
EventName = "ItemTapped",
Command = new Command(() =>
{
executedCommand = true;
})
};
var listView = new ListView();
listView.Behaviors.Add(behavior);
behavior.RaiseEvent(listView, null);
Assert.True(executedCommand);
}
[Fact]
public void CommandCanExecuteTest()
{
var behavior = new MockEventToCommandBehavior
{
EventName = "ItemTapped",
Command = new Command(() => Assert.True(false), () => false)
};
var listView = new ListView();
listView.Behaviors.Add(behavior);
behavior.RaiseEvent(listView, null);
}
[Fact]
public void CommandCanExecuteWithParameterShouldNotExecuteTest()
{
bool shouldExecute = false;
var behavior = new MockEventToCommandBehavior
{
EventName = "ItemTapped",
CommandParameter = shouldExecute,
Command = new Command<string>(o => Assert.True(false), o => o.Equals(true))
};
var listView = new ListView();
listView.Behaviors.Add(behavior);
behavior.RaiseEvent(listView, null);
}
[Fact]
public void CommandWithConverterTest()
{
const string item = "ItemProperty";
bool executedCommand = false;
var behavior = new MockEventToCommandBehavior
{
EventName = "ItemTapped",
EventArgsConverter = new ItemTappedEventArgsConverter(false),
Command = new Command<string>(o =>
{
executedCommand = true;
Assert.NotNull(o);
Assert.Equal(item, o);
})
};
var listView = new ListView();
listView.Behaviors.Add(behavior);
behavior.RaiseEvent(listView, new ItemTappedEventArgs(listView, item));
Assert.True(executedCommand);
}
private class ItemTappedEventArgsConverter : IValueConverter
{
private readonly bool _returnParameter;
public bool HasConverted { get; private set; }
public ItemTappedEventArgsConverter(bool returnParameter)
{
_returnParameter = returnParameter;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
HasConverted = true;
return _returnParameter ? parameter : (value as ItemTappedEventArgs)?.Item;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
}

+ 0
- 27
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/DummyTests.cs View File

@ -1,27 +0,0 @@
using System;
using System.Threading.Tasks;
using Xunit;
namespace eShopOnContainers.UnitTests
{
public class DummyTests
{
[Fact]
public void ThisShouldPass_Sync()
{
Assert.True(true);
}
[Fact]
public async Task ThisShouldPass_Async()
{
await Task.Run(() => { Assert.True(true); });
}
[Fact]
public async Task ThisShouldFail_Async()
{
await Task.Run(() => { throw new Exception("Oops!"); });
}
}
}

+ 12
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Mocks/MockEventToCommandBehavior.cs View File

@ -0,0 +1,12 @@
using eShopOnContainers.Core.Behaviors;
namespace eShopOnContainers.UnitTests
{
public class MockEventToCommandBehavior : EventToCommandBehavior
{
public void RaiseEvent(params object[] args)
{
_handler.DynamicInvoke(args);
}
}
}

+ 53
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Mocks/MockViewModel.cs View File

@ -0,0 +1,53 @@
using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.Core.Validations;
namespace eShopOnContainers.UnitTests
{
public class MockViewModel : ViewModelBase
{
private ValidatableObject<string> _forename;
private ValidatableObject<string> _surname;
public ValidatableObject<string> Forename
{
get
{
return _forename;
}
set
{
_forename = value;
RaisePropertyChanged(() => Forename);
}
}
public ValidatableObject<string> Surname
{
get
{
return _surname;
}
set
{
_surname = value;
RaisePropertyChanged(() => Surname);
}
}
public MockViewModel()
{
_forename = new ValidatableObject<string>();
_surname = new ValidatableObject<string>();
_forename.Validations.Add(new IsNotNullOrEmptyRule<string> { ValidationMessage = "Forename is required." });
_surname.Validations.Add(new IsNotNullOrEmptyRule<string> { ValidationMessage = "Surname name is required." });
}
public bool Validate()
{
bool isValidForename = _forename.Validate();
bool isValidSurname = _surname.Validate();
return isValidForename && isValidSurname;
}
}
}

src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/BasketServiceTests.cs → src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Services/BasketServiceTests.cs View File


src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/CatalogServiceTests.cs → src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Services/CatalogServiceTests.cs View File


src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/OrdersServiceTests.cs → src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/Services/OrdersServiceTests.cs View File

@ -1,6 +1,5 @@
using eShopOnContainers.Core;
using eShopOnContainers.Core.Services.Order;
using eShopOnContainers.Core.Services.RequestProvider;
using System.Threading.Tasks;
using Xunit;
@ -8,6 +7,15 @@ namespace eShopOnContainers.UnitTests
{
public class OrdersServiceTests
{
[Fact]
public async Task GetFakeOrderTest()
{
var ordersMockService = new OrderMockService();
var order = await ordersMockService.GetOrderAsync(1, GlobalSetting.Instance.AuthToken);
Assert.NotNull(order);
}
[Fact]
public async Task GetFakeOrdersTest()
{
@ -16,15 +24,5 @@ namespace eShopOnContainers.UnitTests
Assert.NotEqual(0, result.Count);
}
[Fact]
public async Task GetOrdersTest()
{
var requestProvider = new RequestProvider();
var ordersService = new OrderService(requestProvider);
var result = await ordersService.GetOrdersAsync(GlobalSetting.Instance.AuthToken);
Assert.NotEqual(0, result.Count);
}
}
}

+ 223
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/ViewModels/CatalogViewModelTests.cs View File

@ -0,0 +1,223 @@
using Xunit;
using eShopOnContainers.Core.ViewModels;
using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.Core.Services.Catalog;
using eShopOnContainers.Core.Models.Catalog;
using System.Threading.Tasks;
using System.Linq;
namespace eShopOnContainers.UnitTests
{
public class CatalogViewModelTests
{
public CatalogViewModelTests()
{
ViewModelLocator.RegisterDependencies(true);
}
[Fact]
public void AddCatalogItemCommandIsNotNullTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.NotNull(catalogViewModel.AddCatalogItemCommand);
}
[Fact]
public void FilterCommandIsNotNullTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.NotNull(catalogViewModel.FilterCommand);
}
[Fact]
public void ClearFilterCommandIsNotNullTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.NotNull(catalogViewModel.ClearFilterCommand);
}
[Fact]
public void ProductsPropertyIsNullWhenViewModelInstantiatedTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.Null(catalogViewModel.Products);
}
[Fact]
public void BrandsPropertyuIsNullWhenViewModelInstantiatedTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.Null(catalogViewModel.Brands);
}
[Fact]
public void BrandPropertyIsNullWhenViewModelInstantiatedTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.Null(catalogViewModel.Brand);
}
[Fact]
public void TypesPropertyIsNullWhenViewModelInstantiatedTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.Null(catalogViewModel.Types);
}
[Fact]
public void TypePropertyIsNullWhenViewModelInstantiatedTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.Null(catalogViewModel.Type);
}
[Fact]
public void IsFilterPropertyIsFalseWhenViewModelInstantiatedTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Assert.False(catalogViewModel.IsFilter);
}
[Fact]
public async Task ProductsPropertyIsNotNullAfterViewModelInitializationTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
await catalogViewModel.InitializeAsync(null);
Assert.NotNull(catalogViewModel.Products);
}
[Fact]
public async Task BrandsPropertyIsNotNullAfterViewModelInitializationTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
await catalogViewModel.InitializeAsync(null);
Assert.NotNull(catalogViewModel.Brands);
}
[Fact]
public async Task TypesPropertyIsNotNullAfterViewModelInitializationTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
await catalogViewModel.InitializeAsync(null);
Assert.NotNull(catalogViewModel.Types);
}
[Fact]
public async Task SettingProductsPropertyShouldRaisePropertyChanged()
{
bool invoked = false;
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
catalogViewModel.PropertyChanged += (sender, e) =>
{
if (e.PropertyName.Equals("Products"))
invoked = true;
};
await catalogViewModel.InitializeAsync(null);
Assert.True(invoked);
}
[Fact]
public async Task SettingBrandsPropertyShouldRaisePropertyChanged()
{
bool invoked = false;
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
catalogViewModel.PropertyChanged += (sender, e) =>
{
if (e.PropertyName.Equals("Brands"))
invoked = true;
};
await catalogViewModel.InitializeAsync(null);
Assert.True(invoked);
}
[Fact]
public async Task SettingTypesPropertyShouldRaisePropertyChanged()
{
bool invoked = false;
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
catalogViewModel.PropertyChanged += (sender, e) =>
{
if (e.PropertyName.Equals("Types"))
invoked = true;
};
await catalogViewModel.InitializeAsync(null);
Assert.True(invoked);
}
[Fact]
public void AddCatalogItemCommandSendsAddProductMessageTest()
{
bool messageReceived = false;
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
Xamarin.Forms.MessagingCenter.Subscribe<CatalogViewModel, CatalogItem>(this, MessageKeys.AddProduct, (sender, arg) =>
{
messageReceived = true;
});
catalogViewModel.AddCatalogItemCommand.Execute(null);
Assert.True(messageReceived);
}
[Fact]
public async Task FilterCommandSendsFilterMessageTest()
{
bool messageReceived = false;
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
await catalogViewModel.InitializeAsync(null);
catalogViewModel.Brand = catalogViewModel.Brands.FirstOrDefault();
catalogViewModel.Type = catalogViewModel.Types.FirstOrDefault();
Xamarin.Forms.MessagingCenter.Subscribe<CatalogViewModel>(this, MessageKeys.Filter, (sender) =>
{
messageReceived = true;
});
catalogViewModel.FilterCommand.Execute(null);
Assert.True(messageReceived);
}
[Fact]
public async Task ClearFilterCommandResetsPropertiesTest()
{
var catalogService = new CatalogMockService();
var catalogViewModel = new CatalogViewModel(catalogService);
await catalogViewModel.InitializeAsync(null);
catalogViewModel.ClearFilterCommand.Execute(null);
Assert.Null(catalogViewModel.Brand);
Assert.Null(catalogViewModel.Type);
Assert.NotNull(catalogViewModel.Products);
}
}
}

+ 54
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/ViewModels/MainViewModelTests.cs View File

@ -0,0 +1,54 @@
using Xunit;
using eShopOnContainers.Core.ViewModels;
using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.Core.Models.Navigation;
using System.Threading.Tasks;
namespace eShopOnContainers.UnitTests
{
public class MainViewModelTests
{
public MainViewModelTests()
{
ViewModelLocator.RegisterDependencies(true);
}
[Fact]
public void SettingsCommandIsNotNullWhenViewModelInstantiatedTest()
{
var mainViewModel = new MainViewModel();
Assert.NotNull(mainViewModel.SettingsCommand);
}
[Fact]
public async Task ViewModelInitializationSendsChangeTabMessageTest()
{
bool messageReceived = false;
var mainViewModel = new MainViewModel();
var tabParam = new TabParameter { TabIndex = 2 };
Xamarin.Forms.MessagingCenter.Subscribe<MainViewModel, int>(this, MessageKeys.ChangeTab, (sender, arg) =>
{
messageReceived = true;
});
await mainViewModel.InitializeAsync(tabParam);
Assert.True(messageReceived);
}
[Fact]
public void IsBusyPropertyIsFalseWhenViewModelInstantiatedTest()
{
var mainViewModel = new MainViewModel();
Assert.False(mainViewModel.IsBusy);
}
[Fact]
public async Task IsBusyPropertyIsTrueAfterViewModelInitializationTest()
{
var mainViewModel = new MainViewModel();
await mainViewModel.InitializeAsync(null);
Assert.True(mainViewModel.IsBusy);
}
}
}

+ 113
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/ViewModels/MockViewModelTests.cs View File

@ -0,0 +1,113 @@
using Xunit;
using eShopOnContainers.Core.ViewModels.Base;
namespace eShopOnContainers.UnitTests
{
public class MockViewModelTests
{
public MockViewModelTests()
{
ViewModelLocator.RegisterDependencies(true);
}
[Fact]
public void CheckValidationFailsWhenPropertiesAreEmptyTest()
{
var mockViewModel = new MockViewModel();
bool isValid = mockViewModel.Validate();
Assert.False(isValid);
Assert.Null(mockViewModel.Forename.Value);
Assert.Null(mockViewModel.Surname.Value);
Assert.False(mockViewModel.Forename.IsValid);
Assert.False(mockViewModel.Surname.IsValid);
Assert.NotEmpty(mockViewModel.Forename.Errors);
Assert.NotEmpty(mockViewModel.Surname.Errors);
}
[Fact]
public void CheckValidationFailsWhenOnlyForenameHasDataTest()
{
var mockViewModel = new MockViewModel();
mockViewModel.Forename.Value = "John";
bool isValid = mockViewModel.Validate();
Assert.False(isValid);
Assert.NotNull(mockViewModel.Forename.Value);
Assert.Null(mockViewModel.Surname.Value);
Assert.True(mockViewModel.Forename.IsValid);
Assert.False(mockViewModel.Surname.IsValid);
Assert.Empty(mockViewModel.Forename.Errors);
Assert.NotEmpty(mockViewModel.Surname.Errors);
}
[Fact]
public void CheckValidationPassesWhenOnlySurnameHasDataTest()
{
var mockViewModel = new MockViewModel();
mockViewModel.Surname.Value = "Smith";
bool isValid = mockViewModel.Validate();
Assert.False(isValid);
Assert.Null(mockViewModel.Forename.Value);
Assert.NotNull(mockViewModel.Surname.Value);
Assert.False(mockViewModel.Forename.IsValid);
Assert.True(mockViewModel.Surname.IsValid);
Assert.NotEmpty(mockViewModel.Forename.Errors);
Assert.Empty(mockViewModel.Surname.Errors);
}
[Fact]
public void CheckValidationPassesWhenBothPropertiesHaveDataTest()
{
var mockViewModel = new MockViewModel();
mockViewModel.Forename.Value = "John";
mockViewModel.Surname.Value = "Smith";
bool isValid = mockViewModel.Validate();
Assert.True(isValid);
Assert.NotNull(mockViewModel.Forename.Value);
Assert.NotNull(mockViewModel.Surname.Value);
Assert.True(mockViewModel.Forename.IsValid);
Assert.True(mockViewModel.Surname.IsValid);
Assert.Empty(mockViewModel.Forename.Errors);
Assert.Empty(mockViewModel.Surname.Errors);
}
[Fact]
public void SettingForenamePropertyShouldRaisePropertyChanged()
{
bool invoked = false;
var mockViewModel = new MockViewModel();
mockViewModel.Forename.PropertyChanged += (sender, e) =>
{
if (e.PropertyName.Equals("Value"))
invoked = true;
};
mockViewModel.Forename.Value = "John";
Assert.True(invoked);
}
[Fact]
public void SettingSurnamePropertyShouldRaisePropertyChanged()
{
bool invoked = false;
var mockViewModel = new MockViewModel();
mockViewModel.Surname.PropertyChanged += (sender, e) =>
{
if (e.PropertyName.Equals("Value"))
invoked = true;
};
mockViewModel.Surname.Value = "Smith";
Assert.True(invoked);
}
}
}

+ 55
- 0
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/ViewModels/OrderViewModelTests.cs View File

@ -0,0 +1,55 @@
using Xunit;
using eShopOnContainers.Core;
using eShopOnContainers.Core.ViewModels;
using eShopOnContainers.Core.ViewModels.Base;
using eShopOnContainers.Core.Services.Order;
using System.Threading.Tasks;
namespace eShopOnContainers.UnitTests
{
public class OrderViewModelTests
{
public OrderViewModelTests()
{
ViewModelLocator.RegisterDependencies(true);
}
[Fact]
public void OrderPropertyIsNullWhenViewModelInstantiatedTest()
{
var orderService = new OrderMockService();
var orderViewModel = new OrderDetailViewModel(orderService);
Assert.Null(orderViewModel.Order);
}
[Fact]
public async Task OrderPropertyIsNotNullAfterViewModelInitializationTest()
{
var orderService = new OrderMockService();
var orderViewModel = new OrderDetailViewModel(orderService);
var order = await orderService.GetOrderAsync(1, GlobalSetting.Instance.AuthToken);
await orderViewModel.InitializeAsync(order);
Assert.NotNull(orderViewModel.Order);
}
[Fact]
public async Task SettingOrderPropertyShouldRaisePropertyChanged()
{
bool invoked = false;
var orderService = new OrderMockService();
var orderViewModel = new OrderDetailViewModel(orderService);
orderViewModel.PropertyChanged += (sender, e) =>
{
if (e.PropertyName.Equals("Order"))
invoked = true;
};
var order = await orderService.GetOrderAsync(1, GlobalSetting.Instance.AuthToken);
await orderViewModel.InitializeAsync(order);
Assert.True(invoked);
}
}
}

+ 26
- 4
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/eShopOnContainers.UnitTests.csproj View File

@ -34,11 +34,17 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="BasketServiceTests.cs" />
<Compile Include="CatalogServiceTests.cs" />
<Compile Include="DummyTests.cs" />
<Compile Include="OrdersServiceTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Mocks\MockEventToCommandBehavior.cs" />
<Compile Include="Services\BasketServiceTests.cs" />
<Compile Include="Services\CatalogServiceTests.cs" />
<Compile Include="ViewModels\CatalogViewModelTests.cs" />
<Compile Include="ViewModels\MainViewModelTests.cs" />
<Compile Include="ViewModels\OrderViewModelTests.cs" />
<Compile Include="Services\OrdersServiceTests.cs" />
<Compile Include="Behaviors\EventToCommandBehaviorTests.cs" />
<Compile Include="Mocks\MockViewModel.cs" />
<Compile Include="ViewModels\MockViewModelTests.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
@ -57,6 +63,15 @@
<HintPath>..\..\..\..\packages\xunit.extensibility.execution.2.2.0-beta4-build3444\lib\netstandard1.0\xunit.execution.dotnet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Xamarin.Forms.Core">
<HintPath>..\..\..\..\packages\Xamarin.Forms.2.3.4.231\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Core.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Forms.Platform">
<HintPath>..\..\..\..\packages\Xamarin.Forms.2.3.4.231\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Platform.dll</HintPath>
</Reference>
<Reference Include="Xamarin.Forms.Xaml">
<HintPath>..\..\..\..\packages\Xamarin.Forms.2.3.4.231\lib\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Xaml.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
@ -68,6 +83,12 @@
<Name>eShopOnContainers.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Mocks\" />
<Folder Include="Services\" />
<Folder Include="ViewModels\" />
<Folder Include="Behaviors\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@ -76,4 +97,5 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="..\..\..\..\packages\Xamarin.Forms.2.3.4.231\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets" Condition="Exists('..\..\..\..\packages\Xamarin.Forms.2.3.4.231\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets')" />
</Project>

+ 8
- 7
src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/packages.config View File

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit" version="2.2.0-beta4-build3444" targetFramework="portable46-net451+win81" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="portable46-net451+win81" />
<package id="xunit.assert" version="2.2.0-beta4-build3444" targetFramework="portable46-net451+win81" />
<package id="xunit.core" version="2.2.0-beta4-build3444" targetFramework="portable46-net451+win81" />
<package id="xunit.extensibility.core" version="2.2.0-beta4-build3444" targetFramework="portable46-net451+win81" />
<package id="xunit.extensibility.execution" version="2.2.0-beta4-build3444" targetFramework="portable46-net451+win81" />
<package id="xunit.runner.console" version="2.2.0-beta4-build3444" targetFramework="portable46-net451+win81" developmentDependency="true" />
<package id="Xamarin.Forms" version="2.3.4.231" targetFramework="portable46-net451+win81" />
<package id="xunit" version="2.2.0-beta4-build3444" targetFramework="portable-net451+win81" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="portable-net451+win81" requireReinstallation="True" />
<package id="xunit.assert" version="2.2.0-beta4-build3444" targetFramework="portable-net451+win81" requireReinstallation="True" />
<package id="xunit.core" version="2.2.0-beta4-build3444" targetFramework="portable-net451+win81" requireReinstallation="True" />
<package id="xunit.extensibility.core" version="2.2.0-beta4-build3444" targetFramework="portable-net451+win81" requireReinstallation="True" />
<package id="xunit.extensibility.execution" version="2.2.0-beta4-build3444" targetFramework="portable-net451+win81" requireReinstallation="True" />
<package id="xunit.runner.console" version="2.2.0-beta4-build3444" targetFramework="portable-net451+win81" developmentDependency="true" />
</packages>

Loading…
Cancel
Save