test a derived control's OnKeyPress event

"Eric" <[email protected]>
Newsgroups gmane.comp.windows.dotnet.nunit.user
Message-ID <!&!AAAAAAAAAAAYAAAAAAAAADoe/GS0t01DguRS+0fvNArCgAAAEAAAAOqQLvxIP5FDvUIEDEhxJewBAAAAAA==@cox.net>
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 the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone

_______________________________________________
Nunit-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nunit-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.