Hi Roy,
First of all thank you very much for your support. I also have your book "The art of unit testing", which is excellent.
I've made my basic test sample to work without using Microsoft Silverlight Unit Test Framework, which means I used regular, non-Silverlight test project. What do I have in my project? I have MyControl Silverlight page:
public partial class MyControl : UserControl
{
...
}
with one TextBox control and one Button control on it. The name of TextBox control is txtNumber and the name of Button control is cmdAdd. I have event handlers for both controls, txtNumber_TextChanged for txtNumber control and cmdAdd_Click for cmdAdd control. Also, I set initally cmdAdd control's IsEnabled property to "False". In code-behind, I'm checking in txtNumber_TextChanged event if the text entered is number and if it is cmdAdd button will be enabled. If the text is not a number, cmdAdd control will be disabled. When I click cmdAdd button, the text (number) will be added to the list. The code is here:
public partial class MyControl : UserControl
{
public IList<string> Numbers { get; set; }
public MyControl()
{
InitializeComponent();
Numbers = new List<string>();
}
private void txtNumber_TextChanged(object sender, TextChangedEventArgs e)
{
int number = 0;
string text = this.txtNumber.Text;
if (!int.TryParse(text, out number) || string.IsNullOrEmpty(text))
this.cmdAdd.IsEnabled = false;
else
this.cmdAdd.IsEnabled = true;
}
private void cmdAdd_Click(object sender, RoutedEventArgs e)
{
string text = this.txtNumber.Text;
Numbers.Add(text);
}
}
In my regular MS Test project I have this:
[TestClass]
public class UnitTest1
{
private MyControl myControl;
[TestMethod, SilverlightUnitTest]
public void TestMethod1()
{
myControl = new MyControl();
myControl.txtNumber.Text = "1";
Assert.IsTrue(myControl.cmdAdd.IsEnabled);
}
As you can see I'd like to test if cmdAdd control is enabled or disabled depends on the content of txtNumber control. In this test case, I set proper value (number) and the test should pass. If I set improper value (text), the test shouldn't pass. When I run the test, it fails on this line:
myControl.txtNumber.Text = "1";
with this error message:
"Test method TestProject1.UnitTest1.TestMethod1 threw exception: System.NullReferenceException: Object reference not set to an instance of an object.."
txtNumber is null.
I'm able to test this sucessfully under Microsoft Silverlight Unit Test Framework using this code:
[TestClass]
public class Test : SilverlightTest
{
private MyControl myControl;
[TestInitialize]
public void TestSetup()
{
myControl = new MyControl();
this.TestPanel.Children.Add(myControl);
}
[TestMethod]
[Asynchronous]
public void TestWeCanAddValidNumber()
{
TextBoxAutomationPeer textBoxPeer = new TextBoxAutomationPeer(myControl.txtNumber);
IValueProvider valueProvider = (IValueProvider)textBoxPeer;
ButtonAutomationPeer buttonPeer = new ButtonAutomationPeer(myControl.cmdAdd);
IInvokeProvider buttonInvoker = (IInvokeProvider)buttonPeer;
EnqueueCallback(() => valueProvider.SetValue("1"));
EnqueueConditional(() => buttonPeer.IsEnabled());
EnqueueCallback(() => buttonInvoker.Invoke());
EnqueueTestComplete();
}
So, my question is how to test events in some UI using SilverUnit? How to make my test above to work without using Microsoft Silverlight Unit Test Framework?
Thank you in advance.
Goran