1 / 25

Unit testing MVVM in Silverlight

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.

hinda
Download Presentation

Unit testing MVVM in Silverlight

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit testing MVVM in Silverlight Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

  2. Agenda • Basics of MVVM • Unit Testing • Silverlight Unit Testing framework • StatLight

  3. Basic MVVM View ViewModel Model

  4. Basic MVVM View ViewModel Model

  5. Basic MVVM View ViewModel Model

  6. Basic MVVM View ViewModel Model

  7. Basic MVVM View Databinding Commands ViewModel Methods Events Model

  8. Unit Testing Unit tests are consistent,isolated, fastand repeatable

  9. Unit Testing • Test only small units of work • TestName = Requirement • 1 Assert per test • Self explanatory • Readability over Maintainability

  10. Silverlight Unit Test Framework • Silverlight Toolkit http://silverlight.codeplex.com • Runs in browser, no support for MsTest

  11. Writing Tests Demo

  12. Writing Tests • [TestClass] • public class ExampleTests{ • [TestMethod] • public class Adding_1_And_1_Should_Be_2(){ • int result = 1 + 1; • Assert.AreEqual(2, result); • } • }

  13. Demo Project Silverlight Client GamesView GamesViewModel GamesDataService Server WCF Ria Service Database

  14. Mvvm Demo Project Demo

  15. Demo Project Silverlight Client Unit Test GamesView GamesViewModelTests GamesViewModel IGamesDataService GamesDataService TestGamesDataService Server WCF Ria Service Database

  16. Silverlight Unit testing Demo

  17. [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);}

  18. [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));}

  19. [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);}

  20. [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);}

  21. [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);}

  22. [ExpectedException(typeof(Exception))] • [TestMethod][ExpectedException(typeof(ArgumentNullException))]public void Executing_SubmitCommand_With_Null_Should_Throw_Exception(){RatingViewModel viewModel = newRatingViewModel(newTestGamesDataService());viewModel.SubmitCommand.Execute(null);}

  23. StatLight • Command line tool • http://statlight.codeplex.com • Possible to integrate in TFS build • Continuous mode

  24. StatLight Demo

  25. 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 !

More Related