FrictSLUnitTest.zip
Source
Sample.Silverlight.WCF.Tests.Clumsy.Web
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
Properties
Sample.Silverlight.WCF.Web
Properties
Services
TaskService.svc
Sample.Silverlight.WCF
Infrastructure
Actions
Services
Properties
Shared
Utility
Views
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.ObjectModel;
using System.ServiceModel;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using Sample.Silverlight.WCF.Infrastructure;
using Sample.Silverlight.WCF.Web.Services;
using ITaskService = Sample.Silverlight.WCF.Tests.Clumsy.ServiceReference1.ITaskService;
namespace Sample.Silverlight.WCF.Tests.Clumsy
{
public class TaskManagementViewModelWithMocking : ViewModelBase
{
Task selected;
Func<ITaskService> serviceFactory;
public TaskManagementViewModelWithMocking(Func<ITaskService> serviceFactory)
{
this.serviceFactory = serviceFactory;
Tasks = new ObservableCollection<Task>();
}
public ObservableCollection<Task> Tasks
{
get; private set;
}
public Task Selected
{
get { return selected; }
set
{
selected = value;
NotifyOfPropertyChange(() => Selected);
}
}
public void Activate()
{
var service = serviceFactory();
service.BeginGetAll(ar =>
{
try
{
var all = service.EndGetAll(ar);
foreach (Task each in all)
{
Tasks.Add(each);
}
Selected = Tasks[0];
}
catch (FaultException exc)
{
// ... some exception handling code ...
}
}, null);
}
}
[TestClass]
public class TaskManagementViewModelWithMockingFixture : SilverlightTest
{
[TestMethod]
public void Should_query_service_for_current_tasks_on_first_activation()
{
// arrange
var expectedData = new ObservableCollection<Task>
{
CreateTask("Task1"),
CreateTask("Task2")
};
var mock = MockRepository.GenerateMock<ITaskService>();
var asyncResult = MockRepository.GenerateMock<IAsyncResult>();
// need to setup expectations for both APM methods
mock.Expect(service => service.BeginGetAll(null, null))
.IgnoreArguments().Return(asyncResult);
mock.Expect(service => service.EndGetAll(asyncResult))
.Return(expectedData);
var model = new TaskManagementViewModelWithMocking(() => mock);
// act
model.Activate();
// we need this to actually complete the call
Callback(mock, service => service.BeginGetAll(null, null))(asyncResult);
// assert
Assert.AreEqual(2, model.Tasks.Count);
Assert.AreSame(model.Tasks[0], model.Selected);
}
static AsyncCallback Callback<TService>(TService mock, Action<TService> method)
{
var arguments = mock.GetArgumentsForCallsMadeOn(method);
return (AsyncCallback)arguments[0][0];
}
static Task CreateTask(string description)
{
return new Task {Id = new Guid(), Description = description};
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of use 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.