I’m trying to test DataBinding within a WinForm application.
The following test case fails.
[Test]
public void TestOneWayEntityBinding()
{
//Arrange
var entity = new MyEntity();
entity.SomeProperty = "someValue";
var userControl = new MyUserControl();
const string pattern = @"xxx";
//Act
userControl.textBox.DataBindings.Add("Text", entity, "SomeProperty");
userControl.textBox.Text = pattern;
//Assert
Assert.AreEqual(pattern, entity.SomeProperty);
}
Even explicitely calling b.WriteValue() onto the binding instance b, does not work.
Thanks,
Marcello.
It seems like your are trying to test .Net framework. You shouldn’t write tests for code, that you don’t own. Good option here is acceptance test, that acts on UI (e.g. White) by setting textBox text.
Btw, why
textBoxis public in your user control? Implementation should be hidden. Its good to have property like ‘Pattern’ of typestring. And you should not expose that databinding used inside your control. So, if you don’t want to exercise UI, then good test for your control will be:Ideally would be to mock entity and just verify that SomeProperty was set, because currently test could fail in two reasons: text box is not bound to SomeProperty, or SomeProperty setter works incorrectly.
UPDATE
Look here why binding is not working after setting Text programmatically. In order to update binding when property changes instead of when its validating, use:
UPDATE
Seems like control should be visible for validation. If you don’t want to change DataSourceUpdateMode, and you don’t run your tests on service without UI, then simple ControlTester will help you:
Use it when acting on control under test:
LAST UPDATE:
DataBining is not working for control which has not been created. So here options:
I think last option is the best one. To make DataBinding work in test, you can simply call CreateControl() method: