Your SlideShare is downloading. ×
0
Timmy Kokke
Silverlight / ASP.Net Developer
UNIT4 Internet Solutions
Expression Blend MVP
Basics of MVVM
Unit Testing
Silverlight Unit Testing framework
StatLight
Model
View
ViewModel
Model
View
ViewModel
Model
View
ViewModel
Model
View
ViewModel
Model
View
ViewModel
Databinding
Commands
Methods
Events
Unit tests are
consistent, isolated, fast and repeatable
Test only small units of work
TestName = Requirement
1 Assert per test
Self explanatory
Readability over Maintainability
Silverlight Toolkit
http://silverlight.codeplex.com
Runs in browser, no support for MsTest
[TestClass]
public class ExampleTests{
[TestMethod]
public class Adding_1_And_1_Should_Be_2(){
int result = 1 + 1;
Assert....
ServerSilverlightClient
GamesViewModel
GamesView
GamesDataService
WCF Ria Service
Database
UnitTest
ServerSilverlightClient
GamesViewModel
GamesView
GamesDataService
WCF Ria Service
Database
TestGamesDataService
I...
[TestMethod]
[Tag("Rating")]
public void Setting_Ratings_Should_Set_AverageRating()
{
Rating rating = new Rating();
rating...
[TestMethod]
[Description("Testing implementation of IDataErrorInfo."+
"Empty String Name should return Error.")]
public v...
[TestMethod]
[Asynchronous]
public void Executing_InitializeCommand_Should_Get_Publishers()
{
PublishersViewModel viewMode...
[TestMethod]
[Bug("Submitting is not implemented at this moment.")]
[WorkItem(1543)]
[Asynchronous]
public void Executing_...
[TestMethod]
[Asynchronous]
[Timeout(250)]
public void Executing_GetMoreCommand_Should_Respond_In_250ms()
{
PublishersView...
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Executing_SubmitCommand_With_Null_Should_Throw...
Command line tool
http://statlight.codeplex.com
Possible to integrate in TFS build
Continuous mode
http://silverlight.codeplex.com
http://statlight.codeplex.com
Timmy Kokke
info@timmykokke.com
http://twitter.com/sorskoot
...
Unit Testing MVVM in Silverlight
Unit Testing MVVM in Silverlight
Unit Testing MVVM in Silverlight
Unit Testing MVVM in Silverlight
Upcoming SlideShare
Loading in...5
×

Unit Testing MVVM in Silverlight

3,714

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,714
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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 -> ViewModel
    ViewModel -> Method -> Model -> Events/Delegates -> ViewModel


    All 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. Dit zijn Integration Tests.
  • Test only small units of work
    TestName = Requirement
    1 Assert per test
    Self explanatory
    Readability over Maintainability
    Naamgeving duidelijk -> underscores etc.
    Schrijf parameters als named parameters
    Vermijd dubbele code (DRY) -> gebruik functies als Make_GameViewModel
  • Testframework is part of Silverlight toolkit
    Runs 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 wel mogelijk, 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 = asynchronous
    Many asynchronous tests
    Tests ends when EnqueueTestComplete is called
  • To demonstrate -> uncomment // Thread.Sleep(TimeSpan.FromSeconds(5)); in TestPublishersDataService
  • Demo of statlight
    Running commandline single and continuous
  • Sixin Meeting (9 nov) about Designer-Developer lifecycle and userstories
  • Transcript of "Unit Testing MVVM in Silverlight"

    1. 1. Timmy Kokke Silverlight / ASP.Net Developer UNIT4 Internet Solutions Expression Blend MVP
    2. 2. Basics of MVVM Unit Testing Silverlight Unit Testing framework StatLight
    3. 3. Model View ViewModel
    4. 4. Model View ViewModel
    5. 5. Model View ViewModel
    6. 6. Model View ViewModel
    7. 7. Model View ViewModel Databinding Commands Methods Events
    8. 8. Unit tests are consistent, isolated, fast and repeatable
    9. 9. Test only small units of work TestName = Requirement 1 Assert per test Self explanatory Readability over Maintainability
    10. 10. Silverlight Toolkit http://silverlight.codeplex.com Runs in browser, no support for MsTest
    11. 11. [TestClass] public class ExampleTests{ [TestMethod] public class Adding_1_And_1_Should_Be_2(){ int result = 1 + 1; Assert.AreEqual(2, result); } }
    12. 12. ServerSilverlightClient GamesViewModel GamesView GamesDataService WCF Ria Service Database
    13. 13. UnitTest ServerSilverlightClient GamesViewModel GamesView GamesDataService WCF Ria Service Database TestGamesDataService IGamesDataService GamesViewModelTests
    14. 14. [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); }
    15. 15. [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)); }
    16. 16. [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); }
    17. 17. [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); }
    18. 18. [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); }
    19. 19. [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void Executing_SubmitCommand_With_Null_Should_Throw_Exception() { RatingViewModel viewModel = new RatingViewModel(new TestGamesDataService()); viewModel.SubmitCommand.Execute(null); }
    20. 20. Command line tool http://statlight.codeplex.com Possible to integrate in TFS build Continuous mode
    21. 21. http://silverlight.codeplex.com http://statlight.codeplex.com Timmy Kokke info@timmykokke.com http://twitter.com/sorskoot http://www.timmykokke.com Silverlight and Expression Usergroup http://www.sixin.nl 9 November next meeting !
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×