-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate Dart "testable" code for Flutter testability #1028
Comments
I think that functions that are static in |
Dart "-stubs" testable mode never really worked. The new testing approach is to generate "$prototype" field for mocking in the production version and mark it as "visible for testing". See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Dart "-stubs" testable mode never really worked. The new testing approach is to generate "$prototype" field for mocking in the production version and mark it as "visible for testing". See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated Dart templates to expose a `$prototype` field from types with static functions/propertoes to allow for mocking of these static functions/properties. No changes to functional tests are required. Smoke tests are updated in a separate commit. See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated Dart templates to expose a `$prototype` field from types with static functions/propertoes to allow for mocking of these static functions/properties. No changes to functional tests are required. Smoke tests are updated in a separate commit. See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated Dart templates to expose a `$prototype` field from types with static functions/propertoes to allow for mocking of these static functions/properties. No changes to functional tests are required. Smoke tests are updated in a separate commit. See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Resolves: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated Dart templates to expose a `$prototype` field from types with static functions/propertoes to allow for mocking of these static functions/properties. No changes to functional tests are required. Smoke tests are updated in a separate commit. See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Resolves: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Dart "-stubs" testable mode never really worked. The new testing approach is to generate "$prototype" field for mocking in the production version and mark it as "visible for testing". See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated Dart templates to expose a `$prototype` field from types with static functions/propertoes to allow for mocking of these static functions/properties. No changes to functional tests are required. Smoke tests are updated in a separate commit. See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Resolves: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
* Make generated Dart code testable/mockable Updated Dart templates to expose a `$prototype` field from types with static functions/propertoes to allow for mocking of these static functions/properties. No changes to functional tests are required. Smoke tests are updated in a separate commit. See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com> * Update smoke tests for Dart `$prototype` change Resolves: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
There is a problem with structs with default constructors which may call ffi. The class seems unmockable now. I propose to move constructor to $Impl as well. May be it make sense to do the same for all classes, generated code must look like:
|
"Custom" constructors are already redirected for structs. Only "field-based" constructors need this additional treatment. As classes only ever have "custom" ctors, nothing needs to be done for them. |
Updated DartStruct template to improve testability: * updated instance methods to be redirects to `$prototype`, similarly to how it's done already for static methods. * removed `_copy()` constructor as it was an unnecessary level of indirection that also impeded testability. Smoke tests are updated in a separate commit. See: #1028
Updated DartStruct template to improve testability: * updated instance methods to be redirects to `$prototype`, similarly to how it's done already for static methods. * removed `_copy()` constructor as it was an unnecessary level of indirection that also impeded testability. Updated smoke tests. Functional tests don't need to be updated because there is no change in user-facing functionality. See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated DartStruct template to improve testability: * updated instance methods to be redirects to `$prototype`, similarly to how it's done already for static methods. * removed `_copy()` constructor as it was an unnecessary level of indirection that also impeded testability. Updated smoke tests. Functional tests don't need to be updated because there is no change in user-facing functionality. Resolves: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated DartStruct template to remove `_copy()` constructor. It was an unnecessary level of indirection that also impeded testability. See: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated DartStruct template to improve testability: * updated instance methods to be redirects to `$prototype`, similarly to how it's done already for static methods. * removed `_copy()` constructor as it was an unnecessary level of indirection that also impeded testability. Updated smoke tests. Functional tests don't need to be updated because there is no change in user-facing functionality. Resolves: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated DartStruct template to improve testability: updated instance methods to be redirects to `$prototype`, similarly to how it's done already for static methods. Updated smoke tests. Functional tests don't need to be updated because there is no change in user-facing functionality. Resolves: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Updated DartStruct template to improve testability: updated instance methods to be redirects to `$prototype`, similarly to how it's done already for static methods. Updated smoke tests. Functional tests don't need to be updated because there is no change in user-facing functionality. Resolves: #1028 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
Flutter testing
Flutter unit tests are usually ran on host. Also it's important to disconnect business logic from generated interface to avoid side effects. Following options were investigated.
Dependency injection
There are few external dart libraries which allow to inject dependencies, for example get_it
Unfortunately they require to write or adopt existing code in specific way.
There is no way to replace (inject) package in tests with other package (for example with stubs) like it can be done for Java.
Conditional import
Dart 1.x supported conditional import based on specified compilation flags.
Dart 2.x is not so flexible and supports only limited conditional import based on presence of platform libraries
dart.library.xxx
, likeI haven't found a way to use this approach to make conditional imports only for unit tests.
Override lookupFunction
Since all FFI functions are resolved with
nativeLibrary.lookupFunction
, thislookupFunction
(or wrapper around this function) can be overridden and return mocked functions. Such approach could allow to not touch existing code, but provide generated code as extra plugin for testing which replaces__lib.nativeLibrary
with overridden version.The problem here is to return test data from such mocked functions. The functions work with data which is prepared for ffi call, for example
Pointer<Void>
instead of usual types. ThisPointer<Void>
must be converted back to the instance of type, perhaps with call to existing generated converter function.It looks very costly to generate the code which allows to mock functions in convenient way.
TBD
Factory methods
The common approach is to call factory method to select real implementation or mocking runtime. In unit tests those factory methods must be replaced during fixture setup.
Prototype class
Approach with prototype class looks easier and most convenient.
The requirements after all must be the following.
stub
) is necessaryEach such class must contain static variable
$prototype
. This static variable must be initialized with instance of the$Impl
class. All calls to constructors, static methods, getters, setters must be performed via$prototype
Example of usage:
The text was updated successfully, but these errors were encountered: