<p>I'm convinced from <a href="http://www.masukomi.org/talks/unit_testing_talk_2/index.xul?data=slide_data.txt#page2" rel="nofollow noreferrer">this presentation</a> and other commentary here on the site that I need to learn to Unit Test.  I also realize that there have been many questions about what unit testing is here.  Each time I go to consider how it should be done in the application I am currently working on, I walk away confused.  It is a xulrunner application application, and a lot of the logic is event-based - when a user clicks here, this action takes place.</p>

<p>Often the examples I see for testing are testing classes - they instantiate an object, give it mock data, then check the properties of the object afterward.  That makes sense to me - but what about the non-object-oriented pieces?</p>

<p><a href="https://stackoverflow.com/questions/2364/what-is-your-experience-with-unit-testing-in-practice#2390">This guy mentioned</a> that GUI-based unit testing is difficult in most any testing framework, maybe that's the problem.  The presentation linked above mentions that each test should only touch one class, one method at a time.  That seems to rule out what I'm trying to do.</p>

<p>So the question - how does one unit testing procedural or event-based code?  Provide a link to good documentation, or explain it yourself.</p>

<p>On a side note, I also have a challenge of not having found a testing framework that is set up to test xulrunner apps - it seems that the tools just aren't developed yet.  I imagine this is more peripheral than my understanding the concepts, writing testable code, applying unit testing.</p>

[{'answer_id': 73126, 'author': 'Joel Martinez', 'author_id': 5416, 'author_profile': 'https://Stackoverflow.com/users/5416', 'pm_score': 1, 'selected': False, 'text': '<p>The problem is that "event based programming" links far too much logic to the events.  The way such a system should be designed is that there should be a subsystem that raises events (and you can write tests to ensure that these events are raised in the proper order). And there should be another subsystem that deals only with managing, say, the state of a form.  And you can write a unit test that will verify that given the correct inputs (ie. events being raised), will set the form state to the correct values.</p>\n\n<p>Beyond that, the actual event handler that is raised from component 1, and calls the behavior on component 2 is just integration testing which can be done manually by a QA person.</p>\n'}, {'answer_id': 73156, 'author': 'Gilligan', 'author_id': 12356, 'author_profile': 'https://Stackoverflow.com/users/12356', 'pm_score': 2, 'selected': False, 'text': '<p>At first I would test events like this:</p>\n\n<pre><code>private bool fired;\n\nprivate void HandlesEvent(object sender, EventArgs e)\n{\n    fired = true;\n } \n\npublic void Test()\n{\n   class.FireEvent += HandlesEvent;\n   class.PErformEventFiringAction(null, null);\n\n   Assert.IsTrue(fired);\n}\n</code></pre>\n\n<p>And Then I discovered <a href="http://www.ayende.com/wiki/Rhino+Mocks+Events.ashx" rel="nofollow noreferrer">RhinoMocks</a>.\nRhinoMocks is a framework that creates mock objects and it also handles event testing. It may come in handy for your procedural testing as well.</p>\n'}, {'answer_id': 73169, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>An approach I've found helpful for procedural code is to use TextTest. It's not so much about unit testing, but it helps you do automated regression testing. The idea is that you have your application write a log then use texttest to compare the log before and after your changes. </p>\n"}, {'answer_id': 73239, 'author': 'David Arno', 'author_id': 7122, 'author_profile': 'https://Stackoverflow.com/users/7122', 'pm_score': 4, 'selected': True, 'text': '<p>The idea of unit testing is to test small sections of code with each test. In an event based system, one form of unit testing you could do, would be to test how your event handlers respond to various events. So your unit test might set an aspect of your program into a specific state, then call the event listener method directly, and finally test the subsequent state of of your program. </p>\n\n<p>If you plan on unit testing an event-based system, you will make your life a lot easier for yourself if you use the dependency injection pattern and ideally would go the whole way and use inversion of control (see <a href="http://martinfowler.com/articles/injection.html" rel="nofollow noreferrer">http://martinfowler.com/articles/injection.html</a> and <a href="http://msdn.microsoft.com/en-us/library/aa973811.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa973811.aspx</a> for details of these patterns)</p>\n\n<p>(thanks to pc1oad1etter for pointing out I\'d messed up the links)</p>\n'}, {'answer_id': 85890, 'author': 'Joe Schneider', 'author_id': 1541, 'author_profile': 'https://Stackoverflow.com/users/1541', 'pm_score': 0, 'selected': False, 'text': '<p>See the oft-linked <a href="https://rads.stackoverflow.com/amzn/click/com/0131177052" rel="nofollow noreferrer" rel="nofollow noreferrer">Working Effectively with Legacy Code</a>.  See the sections titled "My Application  Is All API Calls" and "My Project is Not Object-Oriented. How Do I Make Safe Changes?".   </p>\n\n<p>In C/C++ world (my experience) the best solution in practice is to use the linker "seam" and link against test doubles for all the functions called by the function under test. That way you don\'t change any of the legacy code, but you can still test it in isolation.</p>\n'}, {'answer_id': 107273, 'author': 'pc1oad1etter', 'author_id': 525, 'author_profile': 'https://Stackoverflow.com/users/525', 'pm_score': 2, 'selected': False, 'text': '<p>Answering my own question here, but I came across an article that take explains the problem, and does a walk-through of a simple example -- <a href="http://www.onjava.com/pub/a/onjava/2004/11/17/agileuser_1.html" rel="nofollow noreferrer">Agile User Interface Development</a>.  The code and images are great, and here is a snippet that shows the idea:</p>\n\n<blockquote>\n  <p>Agile gurus such as Kent Beck and\n  David Astels suggest building the GUI\n  by keeping the view objects very thin,\n  and testing the layers "below the\n  surface." This "smart object/thin\n  view" model is analogous to the\n  familiar document-view and\n  client-server paradigms, but applies\n  to the development of individual GUI\n  elements. Separation of the content\n  and presentation improves the design\n  of the code, making it more modular\n  and testable. Each component of the\n  user interface is implemented as a\n  smart object, containing the\n  application behavior that should be\n  tested, but no GUI presentation code.\n  Each smart object has a corresponding\n  thin view class containing only\n  generic GUI behavior. With this design\n  model, GUI building becomes amenable\n  to TDD.</p>\n</blockquote>\n'}, {'answer_id': 386348, 'author': 'Kjetil Klaussen', 'author_id': 15599, 'author_profile': 'https://Stackoverflow.com/users/15599', 'pm_score': 1, 'selected': False, 'text': '<p>Your question doesn\'t state your programming language of choice, but mine is C# so I\'ll exemplify using that. This is however just a refinement over Gilligans answer by using anonymous delegates to inline your test code. I\'m all in favor of making tests as readable as possible, and to me that means all test code within the test method;</p>\n\n<pre><code>// Arrange\nvar car = new Car();\nstring changedPropertyName = "";\ncar.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)\n                        {\n                           if (sender == car) \n                                  changedPropertyName = e.PropertyName;\n                        };\n\n// Act\ncar.Model = "Volvo";\n\n// Assert \nAssert.AreEqual("Model", changedPropertyName, \n    "The notification of a property change was not fired correctly.");\n</code></pre>\n\n<p>The class I\'m testing here implements the <code>INotifyPropertyChanged</code> interface and therefore a <code>NotifyPropertyChanged</code> event should be raised whenever a property\'s value has changed.</p>\n'}]
['https://Stackoverflow.com/questions/73078', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/525/']