From: Jim Weirich Date: 2006-06-06T00:57:11+09:00 Subject: Re: Need help with TDD, stubs and mocks cr wrote: > When I finished I decided to write tests to verify each method. > However, the main entry point into the script starts off by doing the > directory traversal which then feeds into every other step. I can't > call the method without running all of its dependent methods (which > essentially runs the whole program). This strikes me as my first clue > that I probably did this wrong. Code not done test first will often be hard to test. I think we just discovered that. > 1. Is it fair to say that I should rewrite bottom-up? That is, start > with methods that are independent of others. When I do TDD, I start with very simple cases and work toward more general cases. Both bottom-up and top-down work, I tend to do outside-in (i.e. start at both the top and bottom and work toward the middle). > 2. Do you generally keep all methods in a class independent of each > other? How do you write, and then test, methods that have > dependencies on each other (and therefore cause side effects)? Dependencies? Like "You Must Call Method A before Method B"? I try to avoid those kinds of dependencies when possible. Sometimes the semantics of the object naturally introduce them (e.g. you usually have to push something in order to pop something from a stack). > 3. How does one mock or stub out a SOAP call? What's the difference > between a stub and a mock? Is there an object that you use to perform the soap call? If so, just replace the object with a Stub or Mock. Stub VS Mock: (Terminalogy varies, but the following is a common distinction) Mocks check their calls for correctness, stubs don't. Stubs are used to provide external behavior that might not exist in a test environment. Mocks are used to verify that you are actually calling particular methods with paricular arguments. Note that using this terminology allows a single object to mock some methods and stub others. I do this alot. If you divide methods into two camps: those that return values (queries) and those that do things (commands), I usually stub queries and mock commands. > 4. How do you test a method that utilizes an instance variable that > was set somewhere else? E.g. #methodA calculates @result. #methodB > retrieves @second_result. #methodC compares @result and @second_result. Construct your test case so that methodA and methodB are called before methodC does its work. Example: def test_comparison thing = Thing.new thing.methodA thing.methodB assert_equal ExpectedResult, thing.methodC end -- Jim Weirich -- Posted via http://www.ruby-forum.com/.