Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Unit Testing MVVM in Silverlight

on

  • 4,204 views

 

Statistics

Views

Total Views
4,204
Views on SlideShare
3,293
Embed Views
911

Actions

Likes
0
Downloads
23
Comments
0

5 Embeds 911

http://www.timmykokke.com 905
http://blog.timmykokke.com 3
http://translate.googleusercontent.com 1
http://www.linkedin.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • View => presentation of the data. Xaml
  • ViewModel => the glue between View and Model.
  • Model => domain object. the actual data/information. Example:contact (name, phone number, address)
  • ViewModel -> Binding -> View -> Binding/Commands/Behaviors ->ViewModelViewModel -> Method -> Model -> Events/Delegates -> ViewModelAll client-server communication is async in Silverlight… makes testing a little more difficult.Testing mainly ViewModel… Sometimes a little View
  • Let op met service calls of andere externe afhankelijkheden. Ditzijn Integration Tests.
  • Test only small units of workTestName = Requirement1 Assert per testSelf explanatoryReadability over Maintainability Naamgevingduidelijk -> underscores etc.Schrijf parameters als named parametersVermijddubbele code (DRY) -> gebruikfunctiesalsMake_GameViewModel
  • Testframework is part of Silverlight toolkitRuns inside the browser, tools available to run from commandline (Statlight)… later more about this.Test classes can inherit from SilverlightTest -> Not supported in MSTest in visual studio (yet?)CodeCoverage is not available yet. Integratie in TFS build process welmogelijk, via custom extension
  • Start new Silverlight Project, add test project. Write some useless tests to demonstrate writing tests… Assert.AreEqual(1+1,3)Attributes:[TestClass][TestMethod]2e demo… Flow van attributes en test methoden… file importeren met example… breakpoints plaatsen[AssemblyInitialize] [AssemblyCleanup] -> 1x per assembly[ClassInitialize] [ClassCleanup] -> 1x per class[TestInitialize] [TestCleanup] -> 1x per test
  • Client is equal for Publisher and Ratings.
  • Show the demo project running for a sec
  • Demo of the various attributes and different unit tests.[Tag][Description][Asynchronous][Bug][WorkItem][TimeOut][ExpectedException]
  • Silverlight = asynchronousMany asynchronous testsTests ends when EnqueueTestComplete is called
  • To demonstrate -> uncomment // Thread.Sleep(TimeSpan.FromSeconds(5)); in TestPublishersDataService
  • Demo ofstatlightRunning commandlinesingle and continuous
  • Sixin Meeting (9 nov) about Designer-Developer lifecycle and userstories

Unit Testing MVVM in Silverlight Unit Testing MVVM in Silverlight Presentation Transcript

  • 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
  • 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 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 !