Thursday, April 19, 2012

Howto Test Databinding with Rhino Mocks and NUnit

I try to test my SW-Project VS2008, .NET 3.5 with "Rhino Mocks" and "NUnit". It uses the MVP Pattern and Databinding, so nothing unusual. But I get a problem (System.ArgumentException) when I try to test the databinding of my controls which occures on System.Windows.Forms.BindToObject.CheckBinding() and is: Cannot bind to the property or column IsOpened on the DataSource. But this only occures in the test. When I run the project everything is fine ... so this might be a problem of my Test definition e.g. Mock definition?



My project looks like the following: (I describe it here with my words and some code. To paste all code here this post will explode, but you can download it here: http://www.speedshare.org/download.php?id=1FD0FF0D11 Password: test)



I've a Presenter:



public class Presenter
{
IView view;
IModel model;

public Presenter(IView view, IModel model)
{
this.view = view;
this.model = model;

view.Shown += new EventHandler(view_Shown);
}

void view_Shown(object sender, EventArgs e)
{
view.OptionsForm.SpecialOptionsUserControl.CheckBox.DataBindings.Add("Checked", model, "IsOpened", false, DataSourceUpdateMode.OnPropertyChanged);

var dialogResult = view.OptionsForm.ShowDialog();
}
}`


which binds the model to the view when it's shown and opens an OptionsDialog. On the OptionsDialog is a UserControl with a Checkbox.
Now the following test should give a little bit of clearance about my problem:



[TestFixture]
public class PresenterTests
{
[Test]
public void ShouldBindCorrectly()
{
var stubView = MockRepository.GenerateStub<IView>();
var stubModel = MockRepository.GenerateStub<IModel>();

var presenterUnderTest = new Presenter(stubView, stubModel);

stubView.Stub(x => x.OptionsForm).Return(new OptionsForm());

stubView.Raise(x => x.Shown += null, this, EventArgs.Empty);
stubModel.AssertWasCalled(x => x.PropertyChanged += Arg<PropertyChangedEventHandler>.Is.Anything);
}
}


Ok, when I execute this test I get an error in line:
var dialogResult = view.OptionsForm.ShowDialog(); described above. Btw. I'm using Rhino Mocks 3.6 and NUnit 2.5.10.



Thanks for your help... (Please let me know if something is missing or could not be understood in my failure description.) Thanks.



Marco





No comments:

Post a Comment