Simple WPF MVVM Application with Unit Testing.
Part-I: Simple Addition of Two Number Using WPF MVVM Pattern
Part-II: Unit testing for same Application
Part-I: Simple Addition of Two Number Using WPF MVVM
Pattern
Folder Structure(Part-I)
Folder: Model - > AdditionofNumbers.cs
public class AdditionofNumbers: PropertychangeNotify
{
private int num1;
public int NUM1
{
get { return num1; }
set { num1 = value;
Notifypropertychange("NUM1"); }
}
private int num2;
public int NUM2
{
get { return num2; }
set { num2 = value;
Notifypropertychange("NUM2"); }
}
private int
addresult;
public int
ADDRESULT
{
get { return
addresult; }
set { addresult = value; Notifypropertychange("ADDRESULT"); }
}
}
Folder: ViewModel - > PropertychangeNotify.cs
public class PropertychangeNotify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler
PropertyChanged;
protected void
Notifypropertychange(string
propertyname)
{
if(PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
}
Folder: ViewModel - > RelayCommand.cs
public class RelayCommand : ICommand
{
public RelayCommand(Action<object>
execute) : this(execute, null)
{
}
public RelayCommand(Action<object>
execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool
CanExecute(object parameter)
{
return _canExecute == null ? true :
_canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
private readonly Action<object>
_execute;
private readonly Predicate<object>
_canExecute;
}
Folder: View - > AdditionViewModel.cs
public class AdditionViewModel: PropertychangeNotify
{
private AdditionofNumbers _additionofNumbers;
private ICommand _AddCommand;
private ICommand _CancelCommand;
public AdditionViewModel()
{
AdditionofNumbers = new AdditionofNumbers();
}
public AdditionofNumbers AdditionofNumbers
{
get
{
return _additionofNumbers;
}
set
{
_additionofNumbers = value;
Notifypropertychange("AdditionofNumbers");
}
}
public ICommand AddCommand
{
get
{
if(_AddCommand==null)
{
_AddCommand = new RelayCommand(param => this.AddNumbers(AdditionofNumbers),
null);
}
return _AddCommand;
}
}
public int
AddNumbers(AdditionofNumbers obj)
{
AdditionofNumbers.ADDRESULT =
obj.NUM1 + obj.NUM2;
return AdditionofNumbers.ADDRESULT;
}
public ICommand CancelCommand
{
get
{
if (_CancelCommand == null)
{
_CancelCommand = new RelayCommand(param => this.CancelOperation(),
null);
}
return _CancelCommand;
}
}
private void
CancelOperation()
{
AdditionofNumbers.NUM1 = 0;
AdditionofNumbers.NUM2 = 0;
AdditionofNumbers.ADDRESULT = 0;
}
}
Folder: View - > MainWindow.xaml
<Window x:Class="WpfAdditionofNumber.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAdditionofNumber"
xmlns:viewmodel="clr-namespace:WpfAdditionofNumber.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewmodel:AdditionViewModel x:Key="ViewModel"/>
</Window.Resources>
<Grid Background="Bisque" Margin="20" DataContext="{Binding Source={StaticResource ViewModel}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Content="Enter
the First Number: " HorizontalAlignment="Right" />
<TextBox Grid.Column="1" Grid.Row="0" Name="Fnumber" Width="100" Height="20" HorizontalAlignment="Left" Text="{Binding AdditionofNumbers.NUM1 , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Column="0" Grid.Row="1" Content="Enter
the Second Number: " HorizontalAlignment="Right" />
<TextBox Grid.Column="1" Grid.Row="1" Width="100" Height="20" HorizontalAlignment="Left" Text="{Binding AdditionofNumbers.NUM2 , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Column="0" Grid.Row="2" Content="Result:
" HorizontalAlignment="Right"
/>
<TextBox Grid.Column="1" Grid.Row="2" Width="100" Height="20" HorizontalAlignment="Left" Text="{Binding AdditionofNumbers.ADDRESULT , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="3" Width="60" Height="30" HorizontalAlignment="Right" Content=" Cancel" Name="Cancelbutton" Command="{Binding CancelCommand}"/>
<Button Grid.Column="1" Grid.Row="3" Width="60" Height="30" HorizontalAlignment="Left" Content=" ADD" Name="Addbutton" Command="{Binding AddCommand}"/>
</Grid>
</Window>
Folder: Output Window
Part-II: Unit Testing
Folder Structure(Part-II & Part-I)
Add New Project in same Solution=> AdditionofNumberTest- > UnitTest1.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WpfAdditionofNumber;
using WpfAdditionofNumber.ViewModel;
using WpfAdditionofNumber.Model;
[TestClass]
public class AdditionofNumbersTest
{
[TestMethod]
public void
AddNumbersTest_20_and_20_return_30()
{
var AdditionVM = new AdditionViewModel();
AdditionofNumbers obj = new AdditionofNumbers();
obj.NUM1 = 20;
obj.NUM2 = 20;
int result = AdditionVM.AddNumbers(obj);
Assert.AreEqual<int>(40, result);
}
}
Comments
Post a Comment