timmy kokke silverlight / asp.net developer unit4 internet solutions expression blend mvp

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

Upload: caleb-amond

Post on 30-Mar-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Unit testing MVVM in Silverlight

Timmy Kokke

Silverlight / ASP.Net DeveloperUNIT4 Internet SolutionsExpression Blend MVP

Page 2: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Agenda

Basics of MVVMUnit TestingSilverlight Unit Testing frameworkStatLight

Page 3: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Basic MVVM

Model

View

ViewModel

Page 4: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Basic MVVM

Model

View

ViewModel

Page 5: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Basic MVVM

Model

View

ViewModel

Page 6: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Basic MVVM

Model

View

ViewModel

Page 7: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Basic MVVM

Model

View

ViewModel

DatabindingCommands

MethodsEvents

Page 8: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Unit Testing

Unit tests are

consistent, isolated, fast and repeatable

Page 9: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Unit Testing

Test only small units of workTestName = Requirement1 Assert per testSelf explanatoryReadability over Maintainability

Page 10: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Silverlight Unit Test Framework

Silverlight Toolkit http://silverlight.codeplex.com

Runs in browser, no support for MsTest

Page 11: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Writing Tests

Demo

Page 12: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Writing Tests

[TestClass]public class ExampleTests{

[TestMethod] public class Adding_1_And_1_Should_Be_2(){ int result = 1 + 1; Assert.AreEqual(2, result); }}

Page 13: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Serv

er

Silv

erlig

ht C

lien

t

Demo Project

GamesViewModel

GamesView

GamesDataService

WCF Ria Service

Database

Page 14: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Mvvm Demo Project

Demo

Page 15: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Un

it Test

Serv

er

Silv

erl

igh

t C

lien

tDemo Project

GamesViewModel

GamesView

GamesDataService

WCF Ria Service

Database

TestGamesDataService

IGamesDataService

GamesViewModelTests

Page 16: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

Silverlight Unit testing

Demo

Page 17: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

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

Page 18: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

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

Page 19: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

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

Page 20: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

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

Page 21: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

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

Page 22: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

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

Page 23: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

StatLight

Command line toolhttp://statlight.codeplex.comPossible to integrate in TFS buildContinuous mode

Page 24: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

StatLight

Demo

Page 25: Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP

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 !