NagRock / ts-mockito

Mocking library for TypeScript
MIT License
974 stars 93 forks source link

How to mock an async function that sets two values in the MockClass? #232

Closed enricocaliolo closed 1 year ago

enricocaliolo commented 1 year ago

I am trying to write some test for an app, but I can't test values stored in a provider. I am relatively new to testing, so there might be something that I am doing wrong, but anyway. What I want to test is to verify is two values are not the same, which should be the expected behavior, but I just can't make it pass. This is the code that I want to test:

class RSAKeysProvider {
    KeyPair? _keyPair;

    KeyPair? get keyPair => _keyPair;
    set setKeyPair(KeyPair keyPair) => _keyPair = keyPair;

    Future<void> generate(String bits) async {
        var keyPair = await RSA.generate(int.parse(bits));
        _keyPair = keyPair;
        notifyListeners();
    }
}

I need to first call the generate() function, which will set the keyPair to actual values, and then check if keyPair.publicKey is different than keyPair.privateKey, but it gives me an error when I try to call generate() with await inside a test.

This is what I have for now, but it doesn't work. The test breaks when it cames to the line "await rsaKeys.generate('2048'). What can I do to make it work? I know the condition is not checking if both are different, but it is just a placeholder, I can't make the code arrive there!

test('Public and private key should be different', () async {
      final MockRSAKeysProvider rsaKeys = MockRSAKeysProvider();

      when(rsaKeys.generate(any)).thenAnswer((value) async {
        KeyPair keyPair = await RSA.generate(2048);
        rsaKeys.setKeyPair = keyPair;
      });

      await rsaKeys.generate('2048');

      expect(rsaKeys.keyPair?.privateKey, isNotNull);
      expect(rsaKeys.keyPair?.publicKey, isNotNull);
    });

I get this error when I try to run this test. Is this Mockito related, or it is something about the package I'm using (fast_rsa)?

Invalid argument(s): Failed to load dynamic library 'librsa_bridge.dylib': dlopen(librsa_bridge.dylib, 0x0001): tried: 'librsa_bridge.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibrsa_bridge.dylib' (no such file), '/opt/homebrew/Caskroom/flutter/3.7.3/flutter/bin/cache/artifacts/engine/darwin-x64/./librsa_bridge.dylib' (no such file), '/opt/homebrew/Caskroom/flutter/3.7.3/flutter/bin/cache/artifacts/engine/darwin-x64/../../../librsa_bridge.dylib' (no such file), '/opt/homebrew/Caskroom/flutter/3.7.3/flutter/bin/cache/artifacts/engine/darwin-x64/Frameworks/librsa_bridge.dylib' (no such file), '/opt/homebrew/Caskroom/flutter/3.7.3/flutter/bin/cache/artifacts/engine/darwin-x64/./librsa_bridge.dylib' (no such file), '/opt/homebrew/Caskroom/flutter/3.7.3/flutter/bin/cache/artifacts/engine/darwin-x64/../../../librsa_bridge.dylib' (no such file), '/opt/homebrew/Caskroom/flutter/3.7.3/flutter/bin/cache/artifacts/engine/darwin-x64/Frameworks/librsa_bridge.dylib' (no such file), '/usr/lib/librsa_bridge.dylib' (no such file, not in dyld cache), 'librsa_bridge.dylib' (no such file), '/usr/lib/librsa_bridge.dylib' (no such file, not in dyld cache)
  dart:ffi                                     new DynamicLibrary.open
  package:fast_rsa/bridge/binding.dart 117:33  Binding.openLib
  package:fast_rsa/bridge/binding.dart 26:16   new Binding._internal
  package:fast_rsa/bridge/binding.dart 17:45   Binding._singleton
  package:fast_rsa/bridge/binding.dart         Binding._singleton
  package:fast_rsa/bridge/binding.dart 22:12   new Binding
  package:fast_rsa/fast_rsa.dart 40:32         RSA.bindingEnabled
  package:fast_rsa/fast_rsa.dart               RSA.bindingEnabled
  package:fast_rsa/fast_rsa.dart 43:9          RSA._call
  package:fast_rsa/fast_rsa.dart 79:22         RSA._keyPairResponse
  package:fast_rsa/fast_rsa.dart 437:18        RSA.generate
  test/rsa_keys.test.dart 32:37                main.<fn>.<fn>.<fn>
  package:mockito/src/mock.dart 185:45         Mock.noSuchMethod
  test/rsa_keys.test.mocks.dart 75:53          MockRSAKeysProvider.generate
  test/rsa_keys.test.dart 36:21                main.<fn>.<fn>