0

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?

Dave Mateer
  • 6,588
  • 15
  • 76
  • 125
  • There is a similar question here: http://stackoverflow.com/questions/5003212/do-i-need-to-call-my-domain-services-context-savechanges-after-adding-a-new-ent – rohancragg Nov 22 '12 at 10:32
  • This looks like it will have some hints too: http://www.silverlightshow.net/items/WCF-RIA-Services-Part-8-Testing-and-Debugging.aspx – rohancragg Nov 22 '12 at 10:57

1 Answers1

0

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.

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
  • 1
    Thanks Spontifixus. This thread really helped me too: http://social.msdn.microsoft.com/Forums/en-US/vstest/thread/5e991b0d-8061-4c4e-a17d-82b4abd58d6c and remember to inherit from SilverlightTest. – Dave Mateer Dec 04 '12 at 16:37
  • @DaveMateer "and remember to inherit from SilverlightTest" - good point, thanks. I added that to my answer. – Spontifixus Dec 04 '12 at 16:43