Working with DependencyObjects in Silverlight Module Tests

I have been writing unit tests using NUnit and Moq with my Silverlight code for some time. One problem I am facing is related to DependencyObjects.

If something comes from DependencyObject, then I cannot create an instance in my test. For example, MouseEventArgs comes from DependencyObject. If I have code that accepts these arguments, I cannot create the arguments for several reasons ... one of them is the DependencyObject.

As far as I understand, the base constructor of DependencyObject is trying to work with some static that does not exist if the entire Silverlight system is not running and running. Any class construction that comes from DependencyObject throws an exception. Bummer.

I do not use the Silverlight Unit Test Framework because it really is not unit testing and requires a user interface. I need real, headless tests.

In any case, the best I've come up with - is to wrap these objects and provide them with the type of interfaces ITimelineMarker, and I give them extension methods for this: timelineMarker.ToInterface(). This works well, and I can mock them ... but I was wondering:

Has anyone come up with a better way to handle DepencencyObjects in Silverlight Unit Tests?

+3
source share
4 answers

, SilverLight? , , - , .

, , , - (, , ), , , - .

, , , MVC, MVP MVVM, .

+3

Silverlight NUnit TestDriven.NET?

0

, DependencyObject .

Rhino mocks , , :

public abstract class MessageBase {
   private List<User> _receivers = new List<User>();
   public void Send() {
      //Some setup
      DetermineReceivers();
      SendMessages();
   }
   private void SendMessages() {
      //Lots of logic
   }
   protected abstract void DetermineReceivers();
   protected void AddReceiver(Group g) {
      //Lots of logic <---- Test this
   }
}

. , :

TestFixture
public sealed class MessageBaseTester {
 public abstract class MockMessageMocker
 {
    public abstract List<Group> RecipientsGroups { get; set; }
 }
 private class MockMessage : MessageBase {
   private MockMessageMocker _mock;
   public MockMessage(MockMessageMocker mock) {
      _mock = mock;
   }
   protected override void DetermineReceivers() {
      if (_mock.RecipientsGroups != null)
         foreach(Group g in _mock.RecipientsGroups)
            base.AddReceiver(g);
   }
 }
}

:

 Test
 public void ShouldblablablaWhenBlabla() {
    var SuThelper = mocks.Stub();
    var SuT = new MockMessage(SuThelper);
    using (mocks.Record())
    {
       Expect.Call(SuTHelper.RecipientsGroups).Return(
             new List<Group>{ new Group(...) });
    }
    using (mocks.Playback())
    {
       SuT.Send();
    }
 }

, . "" .

0
source

Try using SilverUnit for unit testing of Silverlight , as this falsifies the Silverlight infrastructure, it can help with problematic exceptions.

0
source

Source: https://habr.com/ru/post/1706607/


All Articles