Let me just start by saying that I'm new to Unit Testing and it's actually the first time I'm trying to do it.
I am developing an application where a user can add elements to a Canvas
, select them, move them around and so on. On each selected element I add an Adorner
. When the control is deselected the adorner is removed.
I have a method that receives a UIElement
and removes the Adorner
and that is the method I'm testing.
The method I'm using to remove the adorner is:
public static void ClearElementAdorners(UIElement element)
{
IEnumerable<Adorner> a = AdornerLayer.GetAdornerLayer(element).GetAdorners(element).AsEnumerable<Adorner>();
if (a != null)
{
a.ToList().ForEach((p) =>
{
AdornerLayer.GetAdornerLayer(element).Remove(p);
});
}
}
The method I'm using to test is this one.
[TestMethod]
public void ClearElementAdornersUnitTesting()
{
Button el = new Button();
Button el2 = new Button();
TestingWindow t = new TestingWindow();
t.Show();
t.TestingCanvas.Children.Add(el);
t.TestingCanvas.Children.Add(el2);
AdornerLayer alayer = AdornerLayer.GetAdornerLayer(el2);
alayer.Add(new ClassLibrary.EditModeAdornerLayer(el2));
ClassLibrary.AdornerOperations.ClearElementAdorners(el2);
Assert.AreEqual(el, el2);
}
What I'm doing is creating two buttons. One of them gets an adorner and the other one stays as control for the test. I remove the adorner from the element and at the end check if they are equal. I have debugged the method to remove the adorner and I know it's working. What I don't know is how to test it. The Assert.AreEqual always fails with this error.
{"Assert.AreEqual failed. Expected:<System.Windows.Controls.Button>. Actual:<System.Windows.Controls.Button>. "}
Again this may be totally wrong, but how can I run this test? If more information is needed I will gladly provide it.
Thanks
The right way of doing this is:
Adorner[] adorner = AdornerLayer.GetAdornerLayer(el2).GetAdorners(el2);
Assert.IsNull(adorner);