How chuyeow/facon works
Project type and purpose
• This is an open-source Ruby library (a gem) that provides mocking and expectation functionality for Bacon and MacBacon test suites.
• It is not a web app or service; it’s a code library meant to be included in test code to generate mock objects, set expectations on method calls, and verify invocation counts.
File structure
• Top-level files:
• Gemfile, facon.gemspec – define gem dependencies and metadata
• Rakefile – build and test automation
• README.md, Changelog.txt – documentation
• lib/facon – contains the source code modules:
• core_ext (monkey-patch to Object)
• baconize (integration with Bacon)
• error_generator (building error messages)
• expectation (defining expectation objects)
• mock, mockable, proxy (creating and managing mock objects)
• spec – unit tests for each module (baconize_spec, expectation_spec, mock_spec, etc.)
Extract insights from the README
• Facon hooks into Bacon’s lifecycle to install its mocking behavior.
• It uses Ruby metaprogramming (method_missing, proxies) to intercept calls.
• Users write mock('name', :attr => val), call should.receive, then invoke code under test; Facon verifies expectations.
Main components
a. Facon Library Modules
• CoreExt Module (opens Object to add mock method)
• Mockable Module (mix-in for classes/objects)
• Proxy Class (wraps real object or stub data)
• Expectation Class (records method, args, call count rules)
• ErrorGenerator (formats assertion failures)
• Baconize Adapter (registers Facon hooks with Bacon runner)
b. External Interfaces
• Test Code (user’s specs)
• Bacon / MacBacon Framework
• Ruby Runtime
c. Relationships / Data Flow
1. Test code calls mock(…) (via CoreExt).
2. CoreExt creates a Proxy instance, including Mockable.
3. Test code sets expectations on Proxy (calls Expectation APIs).
4. When code under test invokes a mocked method, Proxy delegates to Expectation to record/check calls, possibly returning stubbed values.
5. At the end of each spec, Baconize adapter triggers verification of all expectations.
6. On failure, ErrorGenerator builds descriptive error message and signals Bacon test failure.
d. Architectural patterns and design principles
• Proxy pattern – intercept calls to target objects
• Mixin/monkey-patch – extend Object dynamically
• Separation of concerns – expectation logic, error formatting, Bacon integration each in its own class/module
• Convention over configuration – simple API, default behaviors