260 likes | 370 Views
Unit testing MVVM in Silverlight. Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP. Agenda. Basics of MVVM Unit Testing Silverlight Unit Testing framework StatLight. Basic MVVM. View. ViewModel. Model. Basic MVVM. View. ViewModel. Model.
E N D
Unit testing MVVM in Silverlight Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP
Agenda • Basics of MVVM • Unit Testing • Silverlight Unit Testing framework • StatLight
Basic MVVM View ViewModel Model
Basic MVVM View ViewModel Model
Basic MVVM View ViewModel Model
Basic MVVM View ViewModel Model
Basic MVVM View Databinding Commands ViewModel Methods Events Model
Unit Testing Unit tests are consistent,isolated, fastand repeatable
Unit Testing • Test only small units of work • TestName = Requirement • 1 Assert per test • Self explanatory • Readability 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); • } • }
Demo Project Silverlight Client GamesView GamesViewModel GamesDataService Server WCF Ria Service Database
Mvvm Demo Project Demo
Demo Project Silverlight Client Unit Test GamesView GamesViewModelTests GamesViewModel IGamesDataService GamesDataService TestGamesDataService Server WCF Ria Service Database
[Tag(“anything”)] • [TestMethod][Tag("Rating")]public voidSetting_Ratings_Should_Set_AverageRating(){Rating rating = newRating();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.")]publicvoidGetting_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]publicvoid Executing_InitializeCommand_Should_Get_Publishers(){PublishersViewModel viewModel = newPublishersViewModel( • newTestPublishersDataService());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 = • newRatingViewModel(newTestGamesDataService()); viewModel.SubmitCommand.Execute(null);}
[TimeOut(250)] • [TestMethod] [Asynchronous][Timeout(250)]public void Executing_GetMoreCommand_Should_Respond_In_250ms(){PublishersViewModel viewModel = • new PublishersViewModel(newTestPublishersDataService());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 = newRatingViewModel(newTestGamesDataService());viewModel.SubmitCommand.Execute(null);}
StatLight • Command line tool • http://statlight.codeplex.com • Possible to integrate in TFS build • Continuous mode
StatLight Demo
Unit testing MVVM in Silverlight • http://silverlight.codeplex.com • http://statlight.codeplex.com • Timmy Kokke info@timmykokke.comhttp://twitter.com/sorskoothttp://www.timmykokke.com • Silverlight and Expression Usergroup http://www.sixin.nl 9 November next meeting !