unit testing and mvvm in silverlight
TRANSCRIPT
Unit testing MVVM in Silverlight
Timmy Kokke
Silverlight / ASP.Net DeveloperUNIT4 Internet SolutionsExpression Blend MVP
Agenda
Basics of MVVMUnit TestingSilverlight Unit Testing frameworkStatLight
Basic MVVM
Model
View
ViewModel
Basic MVVM
Model
View
ViewModel
Basic MVVM
Model
View
ViewModel
Basic MVVM
Model
View
ViewModel
Basic MVVM
Model
View
ViewModel
DatabindingCommands
MethodsEvents
Unit Testing
Unit tests are
consistent, isolated, fast and repeatable
Unit Testing
Test only small units of workTestName = Requirement1 Assert per testSelf explanatoryReadability over Maintainability
Silverlight Unit Test Framework
Silverlight Toolkit http://silverlight.codeplex.com
Runs in browser, no support for MsTest
Writing Tests
Demo
Writing Tests
[TestClass]public class ExampleTests{
[TestMethod] public class Adding_1_And_1_Should_Be_2(){ int result = 1 + 1; Assert.AreEqual(2, result); }}
Serv
er
Silv
erlig
ht C
lien
t
Demo Project
GamesViewModel
GamesView
GamesDataService
WCF Ria Service
Database
Mvvm Demo Project
Demo
Un
it Test
Serv
er
Silv
erl
igh
t C
lien
tDemo Project
GamesViewModel
GamesView
GamesDataService
WCF Ria Service
Database
TestGamesDataService
IGamesDataService
GamesViewModelTests
Silverlight Unit testing
Demo
[Tag(“anything”)]
[TestMethod][Tag("Rating")]public void Setting_Ratings_Should_Set_AverageRating(){ Rating rating = new Rating();
rating.GameplayRating = 4; rating.GraphicsRating = 2;
Assert.AreEqual(3, rating.AverageRating);}
[Description(“what’s goin’ on”)]
[TestMethod][Description("Testing implementation of IDataErrorInfo."+ "Empty String Name should return Error.")]public void Getting_ErrorInfo_For_Empty_Name_Should_Return_Error(){ Rating errorInfo = new Rating {Name = string.Empty}; string result = errorInfo["Name"]; Assert.IsFalse(string.IsNullOrWhiteSpace(result));}
[Asynchonous]
[TestMethod][Asynchronous]public void Executing_InitializeCommand_Should_Get_Publishers(){ PublishersViewModel viewModel = new PublishersViewModel( new TestPublishersDataService()); viewModel.PropertyChanged += (s, a) =>
{ Assert.IsTrue(viewModel.Publishers != null); EnqueueTestComplete(); }; viewModel.InitializeCommand.Execute(null);}
[Bug(“Known failure”)][WorkItem(1234)][TestMethod][Bug("Submitting is not implemented at this moment.")][WorkItem(1543)] [Asynchronous]public void Executing_SubmitCommand_Should_Navigate_To_GamesList(){ DecoupledEventAggregator.GetInstance() .GetEvent<NavigateToEvent>().Subscribe((a) => { Assert.IsTrue(true); EnqueueTestComplete(); }); RatingViewModel viewModel = new RatingViewModel(new TestGamesDataService());
viewModel.SubmitCommand.Execute(null);}
[TimeOut(250)]
[TestMethod] [Asynchronous][Timeout(250)]public void Executing_GetMoreCommand_Should_Respond_In_250ms(){ PublishersViewModel viewModel = new PublishersViewModel(new TestPublishersDataService());
viewModel.PropertyChanged += (s, a) => { Assert.IsTrue(true); EnqueueTestComplete(); }; viewModel.GetMoreCommand.Execute(null);}
[ExpectedException(typeof(Exception))]
[TestMethod][ExpectedException(typeof(ArgumentNullException))]public void Executing_SubmitCommand_With_Null_Should_Throw_Exception(){ RatingViewModel viewModel = new RatingViewModel(new TestGamesDataService());
viewModel.SubmitCommand.Execute(null);}
StatLight
Command line toolhttp://statlight.codeplex.comPossible to integrate in TFS buildContinuous mode
StatLight
Demo
Unit testing MVVM in Silverlighthttp://silverlight.codeplex.comhttp://statlight.codeplex.com
Timmy Kokke [email protected] http://twitter.com/sorskoot http://www.timmykokke.com
Silverlight and Expression Usergroup http://www.sixin.nl 9 November next meeting !