繁体   English   中英

观察继承的 class 的 ReactiveObject.Changed 可能有效也可能无效

[英]Observe ReactiveObject.Changed of inherited class may or may not work

我尝试创建一个继承自 ReactiveObject 的基础 class。 此 class 需要检查是否有任何属性(包括进一步继承的属性)已更改。

我的主要问题是,我无法获得任何可靠的测试结果。 提供的测试可能是绿色的,使用 VS Live Unit Testing 运行它。 把它变成红色。 将测试项目中的类移到库中可能会也可能不会使这个测试变绿。

到目前为止,保持它绿色的唯一方法是在继承的 class 中取消注释此行。

//  Changed.Subscribe(Console.WriteLine);
   [TestClass]
   public class StateBaseTests
   {
      #region Tests

      [TestMethod]
      public void State_ChangesToChanged_PropertyChanged()
      {
         var _item = new MyTest();

         _item.Surname = "Testname";

         Assert.AreEqual(Statetype.Changed, _item.State);
      }

      #endregion

      #region SubClasses

      public enum Statetype
      {
         Added,
         Changed,
         Deleted,
         Detached,
         Unchanged
      }

      public abstract class StateBase : ReactiveObject
      {
         protected StateBase()
         {
            State = Statetype.Unchanged;

            Changed.Select(prop => prop.PropertyName)
                   .Subscribe(UpdateZustand);
         }

         protected void UpdateZustand(string propertyName)
         {
            if(State == Statetype.Unchanged)
               State = Statetype.Changed;
         }

         [Reactive]
         public Statetype State { get; set; }
      }

      private class MyTest : StateBase
      {
         public MyTest()
         {
            //  Changed.Subscribe(Console.WriteLine);
         }

         [Reactive]
         public string Name { get; set; }

         [Reactive]
         public string Surname { get; set; }
      }
   }

这是您的样本的轻微改编。 使用 VS Mac Community 和 NUnit 完成的。 每次在我的机器上测试都会变成绿色。 如您所见,还打印了属性名称。 在我的控制台上它输出:

State
Surname

这是代码:

using System;
using System.Reactive.Linq;
using NUnit.Framework;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;

namespace Testy {
    [TestFixture]
    public class Test {
        [Test]
        public void State_ChangesToChanged_PropertyChanged() {
            var _item = new MyTest();
            _item.Surname = "Testname";
            Assert.AreEqual(Statetype.Changed, _item.State);
        }
    }

    public enum Statetype {
        Added,
        Changed,
        Deleted,
        Detached,
        Unchanged
    }

    public abstract class StateBase : ReactiveObject {
        protected StateBase() {
            State = Statetype.Unchanged;

            Changed
                .Select(prop => prop.PropertyName)
                .Subscribe(
                    name => {
                        UpdateZustand(name);
                        Console.WriteLine(name);
                    });
        }

        protected void UpdateZustand(string propertyName) {
            if (State == Statetype.Unchanged)
                State = Statetype.Changed;
        }

        [Reactive]
        public Statetype State { get; set; }
    }

    public class MyTest : StateBase {
        [Reactive]
        public string Name { get; set; }

        [Reactive]
        public string Surname { get; set; }
    }
}

不确定这是否有帮助,但尝试一下很有趣:)。

PS:这些是使用的 package 版本:

  • ReactiveUI(.Fody) 10.5.1
  • Nunit 3.12.0
  • NUnit3TestAdapter 3.15.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM