Integration test:
UERDomainService uerDomainService;
[TestInitialize]
public void Setup()
{
uerDomainService = new UERDomainService();
}
[TestMethod]
public void GetUsersWithRoles_GivenRoleID1003_ShouldNotReturnMateerAsSoftDeleted()
{
// blah
Assert.AreEqual(0, thing.Count());
// blah
uerDomainService.DeleteRoleMembership(rm);
}
then in DeleteRoleMembership(rm) which is a RIA Services code genned method:
public void DeleteRoleMembership(RoleMembership roleMembership)
{
if ((roleMembership.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(roleMembership, EntityState.Deleted);
}
else
{
this.ObjectContext.RoleMemberships.Attach(roleMembership);
this.ObjectContext.RoleMemberships.DeleteObject(roleMembership);
}
// added to get tests working
ObjectContext.SaveChanges();
}
Why do I have to put in this in to get my tests to work?
Yet don't need it for my Silverlight app to work. I know its something to do with the saving pipeline for RIA. The method uerDomainService.Submit needs a ChangeSet.
Question: How do I kick off the SubmitChanges pipeline from my test?
Note that you can't (or should that is to say) simply test your Silverlight code in standard unit tests, due to the asynchronous nature of Silverlight and the calls to the underlying RIA-services.
To be able to test your code create a new "Silverlight Unit Test Application" to your solution. A default test will be included, that basically will look like this (note that the test class is derived from SilverlightTest
-class):
[TestClass]
public class SomeTests : SilverlightTest
{
[TestMethod]
[Asynchronous]
public void CanDownloadDataThenChangeAndSubmitChanges()
{
var target = new YourDomainContext();
// Arrange
this.EnqueueCallback(() => target.Load(target.GetSomethingsQuery()));
this.EnqueueConditional(() => !target.IsLoading);
this.EnqueueCallback(() => Assert.IsTrue(target.Somethings.Any()));
// Act
this.EnqueueCallback(() => target.Somethings.First().SomeProperty = "NewValue");
this.EnqueueCallback(() => target.SubmitChanges());
// Assert
this.EnqueueConditional(() => !target.IsSubmitting));
this.EnqueueCallback(() => Assert.IsFalse(target.HasChanges));
this.EnqueueTestComplete();
}
}
Have a look at this article on CodeProject for some more detailed information.