Ruby Best Practices
- p 9: “The process of problem discovery, refactoring and iterative design are the true fruits of test-driven-development” Testing fundamentals
- keep your tests atomic if you are testing a function with multiple interfaces, write multiple examples
- don’t just check inputs and outputs, also look if exceptions are thrown under the right conditions
- use rake tasks to automate the building of the test-suite
Advanced Testing Techniques
- Mimicks and stubs can be used to remove external dependencies from test while still verifying proper behavior and interaction mocks are used to to create objects that can act in place of external resource the purpose of testing. Mock objects are set up with expected responses, which are then verified when the tests are run
- stubs are used when we want to replace some functionality with canned results to make testing other code easier when testing complex output, it is best to find a tool for parsing your output format (if this is not possible you write a parser that only parses the values you want, if this isn’t possible with easy effort, then write the expected value in a text file and load the example
- use nokogiri to parse XML-Formats
Line-Based File Processing with State Tracking
- identify the beginning and end markers of sections with a pattern if sections are nested, maintain a stack that you updated before further processing of each line
- break up your extraction code into different cases and select the right one based on the current section you are in
Regular Expressions
- are a special language for find-and-replace operations build on simple logical constructs there are a lot of shortcuts available which will make your expressions more readable
Laziness Can be A Virtue
- Lazy evaluation can be useful when you have some code that may never need to be run, or would best be run as late as possible all code blocks in ruby are lazy and are not expected until they are explicitly called