Re: test a derived control's OnKeyPress event
"Charlie Poole" <[email protected]>
| Newsgroups | gmane.comp.windows.dotnet.nunit.user |
|---|---|
| Message-ID | <000201c8b99f$af7ea3b0$660d0c0a@ferrari> |
You could use pInvoke to call SendMessage. Charlie _____ From: [email protected] [mailto:[email protected]] On Behalf Of Eric Sent: Tuesday, May 06, 2008 12:49 AM To: [email protected] Subject: [Nunit-users] test a derived control's OnKeyPress event Anyone got a quick way to send a simulated key press to a control? TIA, Eric [Test] public void NumericInputIsAccepted() { NumericTextBox ntb = new NumericTextBox(); char testChar = 'x'; //SendKeys.Send(tesetChar); <============= send the key press to the text box?? Assert.That(ntb.Text.Equals( string.Empty), "OnKeyPress screened out the non-numeric char"); testChar = '1'; Assert.That(ntb.Text.Equals(testChar), "OnKeyPress let the numeric char in"); } _____ From: [email protected] [mailto:[email protected]] On Behalf Of Eric Sent: Saturday, April 19, 2008 12:27 PM To: [email protected] Subject: [Nunit-users] best way to reuse a set of tests I want to reuse a test that verifies a windows form closes properly, and the only thing that varies would be a given form, which also must be an IClosingWindowView, highlighted below. Sorry to ask what sounds like a basic question, but I'm basically stumped on this right now. Code is below. TIA, Eric using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; using System.Windows.Forms; namespace ef.GUI { [TestFixture] public class ClosingWindowTest { private IClosingWindowView _view; private Form _form; private ClosingWindowPresenter _presenter; private MockYesNoMessageAsker _mockYesNoAsker; [SetUp] public void SetUp() { _setupYesNoMessageAsker(); _setUpIClosingWindowView(); _setupPresenter(); } private void _setupPresenter() { _presenter = new ClosingWindowPresenter(_view, _mockYesNoAsker); Assert.That(_form.IsDisposed, Is.False); Assert.That(_mockYesNoAsker.WasInvoked, Is.False); } private void _setupYesNoMessageAsker() { _mockYesNoAsker = new MockYesNoMessageAsker(); } /// <summary> /// Set up view; we want the Form just to check it has been closed (IsDisposed), /// but the Presenter never has any knowledge of anything to do with forms, just the IView. /// </summary> private void _setUpIClosingWindowView() { _form = new frmClosingWindow(); <------- the thing that would vary across tests _view = _form as IClosingWindowView; } [Test] public void CloseTheScreen_WhenTheScreenIsNotDirty() { // setup unique to this test _view.IsDirty = false; // trigger state changing behavior _presenter.Close(); // validate expectation(s) // - the msg box is not invoked Assert.That(_mockYesNoAsker.WasInvoked, Is.False); // - the window closes Assert.That(_form.IsDisposed, Is.True); } [Test] public void CloseTheScreen_WhenTheScreenIsDirty_UserContinuesCancelAndDiscardsChanges() { // setup unique to this test _view.IsDirty = true; _mockYesNoAsker.DidTheMockUserClickOk = true; // trigger state changing behavior _presenter.Close(); // validate expectation(s) // - the msg box IS invoked Assert.That(_mockYesNoAsker.WasInvoked, Is.True); // - the window closes Assert.That(_form.IsDisposed, Is.True); } [Test] public void CloseTheScreen_WhenTheScreenIsDirty_UserCancelsClosing() { // setup unique to this test _view.IsDirty = true; _mockYesNoAsker.DidTheMockUserClickOk = false; // trigger state changing behavior _presenter.Close(); // validate expectation(s) // - the msg box IS invoked Assert.That(_mockYesNoAsker.WasInvoked, Is.True); // - the window stays open Assert.That(_form.IsDisposed, Is.False); } } } ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Nunit-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/nunit-users