Click here to Skip to main content
11,639,765 members (60,452 online)
Click here to Skip to main content
Add your own
alternative version

Frictionless WCF service consumption in Silverlight - Part 3: Benefits of transparent asynchrony with respect to unit testing

, 26 May 2011 MIT 12.5K 243 11
Simplifying unit testing of View Models which use asynchronous WCF service calls.
FrictSLUnitTest.zip
Source
Sample.Silverlight.WCF
Infrastructure
Actions
Services
Properties
Shared
Utility
Views
Sample.Silverlight.WCF.Tests
Properties
Sample.Silverlight.WCF.Tests.Clumsy
Properties
DataSources
Sample.Silverlight.WCF.Web.Services.Task.datasource
Service References
ServiceReference1
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
TaskService.disco
TaskService.wsdl
ServiceReferences.ClientConfig
Sample.Silverlight.WCF.Tests.Clumsy.Web
Properties
Sample.Silverlight.WCF.Web
Properties
Services
TaskService.svc
Tools
NUnit
nunit.framework.dll
nunit.silverlight.dll
Rhino.Mocks
Castle.Core-Silverlight.dll
Castle.DynamicProxy-Silverlight.dll
Rhino.Mocks 3.5.Silverlight.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

using NUnit.Framework;
using Rhino.Mocks;

using Sample.Silverlight.WCF.Infrastructure.Actions;
using Sample.Silverlight.WCF.Infrastructure.Services;
using Sample.Silverlight.WCF.Web.Services;

namespace Sample.Silverlight.WCF.Views
{
	[TestFixture]
	public class TaskManagementViewModelFixture
	{
		ITaskService mock;
		TaskManagementViewModel model;

		[SetUp]
		public void SetUp()
		{
			mock = MockRepository.GenerateMock<ITaskService>();
			model = new TaskManagementViewModel(mock);
		}

		[Test]
		public void On_activate()
		{
			// arrange
			var tasks = new[] { CreateTask("Task1"), CreateTask("Task2") };
			mock.Expect(service => service.GetAll()).Return(tasks);

			// act
			Execute(model.Activate());

			// assert
			Assert.AreEqual(tasks.Length, model.Tasks.Count);
			Assert.AreSame(model.Tasks[0], model.Selected);
		}

		[Test]
		public void On_create()
		{
			// arrange
			model.Description = "test";
			var newTask = CreateTask(model.Description);

			// new Task instance will be returned from service
			mock.Expect(service => service.Create(model.Description))
				.Return(newTask);

			// act
			Execute(model.Create());

			// assert
			Assert.AreSame(model.Tasks[0], newTask);
			Assert.AreSame(model.Tasks[0], model.Selected);
		}

		[Test]
		public void On_mark_complete()
		{
			// arrange
			var task = CreateTask("Task");
			model.Tasks.Add(task);
			model.Selected = task;

			var updatedTask = CreateTask(task.Id, "Task");
			updatedTask.IsCompleted = true;

			// should pass id of currently selected task
			mock.Expect(service => service.MarkComplete(task.Id))
				.Return(updatedTask);
				
			// act
			Execute(model.MarkComplete());

			// assert
			Assert.AreSame(model.Tasks[0], updatedTask, "Should replace task for updated one");
			Assert.AreSame(model.Selected, updatedTask, "Should set updated one as selected");
		}

		[Test]
		public void On_delete()
		{
			// arrange
			var task1 = CreateTask("Task1");
			var task2 = CreateTask("Task2");

			model.Tasks.Add(task1);
			model.Tasks.Add(task2);

			model.Selected = task2;

			// should pass id of currently selected task
			mock.Expect(service => service.Delete(task2.Id));

			// act
			Execute(model.Delete());

			// assert
			Assert.AreEqual(model.Tasks.Count, 1);
			Assert.AreSame(model.Tasks[0], model.Selected);
		}

		static Task CreateTask(string description)
		{
			return CreateTask(Guid.NewGuid(), description);
		}

		static Task CreateTask(Guid id, string description)
		{
			return new Task { Id = id, Description = description };
		}

		static void Execute(IEnumerable<IAction> routine)
		{
			foreach (var action in routine)
			{
				action.Execute();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Yevhen Bobrov
Software Developer http://blog.xtalion.com
Ukraine Ukraine
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 26 May 2011
Article Copyright 2011 by Yevhen Bobrov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid