RIA服务 - 从测试中调用SaveChanges的地方

时间:2012-11-22 10:13:29

标签: silverlight testing wcf-ria-services

集成测试:

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);
        }

然后在DeleteRoleMembership(rm)中,这是一个RIA服务代码生成方法:

 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();
        }

为什么我必须加入以使我的测试工作?

然而,我的Silverlight应用程序无需工作。我知道它与RIA的保存管道有关。方法uerDomainService.Submit需要一个ChangeSet。

问题:如何从测试中启动SubmitChanges管道?

1 个答案:

答案 0 :(得分:0)

请注意,由于Silverlight的异步特性以及对底层RIA服务的调用,您不能(或者应该说)在标准单元测试中简单地测试您的Silverlight代码。

为了能够测试您的代码,请为您的解决方案创建一个新的“Silverlight单元测试应用程序”。将包含一个默认测试,基本上看起来像这样(注意测试类派生自SilverlightTest - 类):

[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();
    }
}

在CodeProject上查看this article以获取更多详细信息。