Automated Mock Function Generation for JavaScript Unit Testing

Resolve Bottlenecks,
Find Innovative Solutions
Generate Solutions

Solution Overview

Problem

Testing methods and functions in dynamic programming languages like JavaScript are tedious and time-consuming, especially when dealing with hundreds or thousands of methods in large programs, as existing techniques require manual abstraction of external code and creation of mock objects.

Innovation Solution

Automatically creating mock functions during runtime to replace external functions in a target function, allowing for comparison of expected and actual results, including argument values, context state, and error conditions, thereby speeding up the testing process while ensuring high accuracy.

Engineering Contradictions & Design Principles

VSEngineering Contradiction Analysis

1Measurement precision

If manual abstraction of external code and creation of mock objects is used for testing, then testing accuracy can be maintained, but testing time and effort increase significantly

Engineering Contradiction:
Improvetesting accuracyVSAvoidtesting time
Core Design Contradiction:
Measurement precisionVSLoss of time

Solution Approach 1:

The testing system performs self-service by automatically generating mock objects and abstracting external code without requiring manual programmer intervention. The system instruments the target code, identifies external function calls, and automatically creates mock implementations, allowing the testing framework to serve itself rather than relying on manual test creation.

Inventive Principle:
Principle #25Self-service

Solution Approach 2:

The system performs preliminary action by automatically instrumenting and preparing the target code before testing begins. It pre-identifies all external function calls, pre-generates mock objects, and pre-configures the testing environment, so that when actual testing occurs, all preparatory work has already been completed automatically.

Inventive Principle:
Principle #10Preliminary action

2Reliability

If comprehensive unit testing of all methods is performed, then programming errors are minimized, but the complexity and effort of writing tests increases

Engineering Contradiction:
Improveerror minimizationVSAvoidtest writing complexity
Core Design Contradiction:
ReliabilityVSDevice complexity

Solution Approach 1:

The system creates copies of external function calls as mock objects. Instead of requiring programmers to manually create complex test scenarios, the system automatically copies the structure and behavior of external functions into mock implementations that can be easily controlled and observed during testing.

Inventive Principle:
Principle #26Copying

Solution Approach 2:

The testing system provides universal functionality that works across all methods and external functions automatically. The instrumentation framework universally identifies and mocks any external function call, regardless of the specific method being tested, eliminating the need for method-specific test writing complexity.

Inventive Principle:
Principle #6Universality (Multi-functionality)

3Manufacturing precision

If automated test programs are written to test each method separately, then testing thoroughness is improved, but the time required to write and execute tests increases

Engineering Contradiction:
Improvetesting thoroughnessVSAvoidtesting efficiency
Core Design Contradiction:
Manufacturing precisionVSProductivity

Solution Approach 1:

The system merges the code instrumentation, mock object generation, and test execution into a single automated process. Instead of requiring separate manual steps for each of these tasks, the framework combines them into one unified operation that automatically instruments code, generates mocks, and executes tests in sequence.

Inventive Principle:
Principle #5Merging (Combining)

Solution Approach 2:

The system replaces the mechanical process of manual test writing with an automated computational system. The instrumentation framework automatically analyzes code structure, identifies external calls, generates mock implementations, and executes tests without requiring manual mechanical intervention for each testing step.

Inventive Principle:
Principle #28Mechanics substitution (Replace mechanical system)

Data Source

PatentUS8799875B2Streamlining unit testing through hot code swapping
Publication Date: 2014.08.05 ORACLE INT CORP
  • US8799875B2 patent drawing
  • US8799875B2 patent drawing
  • US8799875B2 patent drawing

AI summary

Methods and systems are described for testing methods and other functions by swapping external functions for mock functions. A test case definition is entered by a programmer, which can specify the expected number of arguments and argument values of external function or method calls, the context state, the return value, and any expected error conditions. The test case definition is used by a function tester to automatically create mock functions, modify the target function so that it calls the mock functions, execute the target function with specified arguments and a test context, and then compare the results to expected results.