Coder Social home page Coder Social logo

Comments (16)

alextekartik avatar alextekartik commented on July 30, 2024 1

@rolurq The mocks here are here for testing that the correct calls are made to the plugins. You might want a simpler mock just by implementing DatabaseFactory:

import 'dart:async';

import 'package:sqflite/sqflite.dart';

class DatabaseFactoryMock implements DatabaseFactory {
  @override
  Future<bool> databaseExists(String path) async {
    return false;
  }

  @override
  Future<void> deleteDatabase(String path) async {}

  @override
  Future<String> getDatabasesPath() async {
    return null;
  }

  @override
  Future<Database> openDatabase(String path, {OpenDatabaseOptions options}) {
    return null;
  }
}

final databaseFactoryMock = DatabaseFactoryMock();

and use databaseFactoryMock instead of databaseFactory in your test code.

Then you might say that you need a real Database object so you will need to mock Database then but here it will depends on your need so it is hard to make something that everybody will agree on...

from sqflite.

nobimac avatar nobimac commented on July 30, 2024 1

i also getting this error while testing..

from sqflite.

alextekartik avatar alextekartik commented on July 30, 2024 1

I think setMockMethodCallHandler is deprecated in the next flutter release. A solution is available here:
https://github.com/tekartik/sqflite/blob/master/sqflite_common_ffi/doc/testing.md

Example:

import 'package:flutter_test/flutter_test.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:sqflite/sqflite.dart';

Future main() async {
  // Setup sqflite_common_ffi for flutter test
  setUpAll(() {
    // Initialize FFI
    sqfliteFfiInit();
    // Change the default factory
    databaseFactory = databaseFactoryFfi;
  });
  test('Simple test', () async {
    var db = await openDatabase(inMemoryDatabasePath, version: 1,
        onCreate: (db, version) async {
      await db
          .execute('CREATE TABLE Test (id INTEGER PRIMARY KEY, value TEXT)');
    });
    // Insert some data
    await db.insert('Test', {'value': 'my_value'});
    // Check content
    expect(await db.query('Test'), [
      {'id': 1, 'value': 'my_value'}
    ]);

    await db.close();
  });
}

from sqflite.

alextekartik avatar alextekartik commented on July 30, 2024

I don't think we can run test with plugins as the tests don't run on the device. As a TDD fan, I miss this feature too. At least I did not find a way to do it. None of the "official" flutter plugins have any as far as I can tell and according to https://stackoverflow.com/questions/46382789/how-to-run-tests-with-code-that-depends-on-plugins there is no solution. I remember seeing an example of native code and flutter driver somewhere but I could not make it work.

If you can leave with mocking the API, you could take a look at what I did here (it uses some private source so might break): https://github.com/tekartik/sqflite/blob/master/test/src_database_test.dart if that could help (but it might be a pain to mock return values from the plugin)

from sqflite.

newlix avatar newlix commented on July 30, 2024

hi, can you provide some suggestions to do testing with sqflite? Do I have to run flutter drive?
Thank you

from sqflite.

alextekartik avatar alextekartik commented on July 30, 2024

I'm not aware of any way of doing dart unit-testing with plugins. flutter drive could be a (currently painful) solution. Even plugins in https://github.com/flutter/plugins/ don't have these kinds of test. I'm still waiting for a solution

from sqflite.

nathansamson avatar nathansamson commented on July 30, 2024

Can we not provide a native backend as well where it handles sqlite calls to https://pub.dartlang.org/packages/sqlite?

I understand this might basically triple the work needed for implementation, but I think it might be worth it...

from sqflite.

alextekartik avatar alextekartik commented on July 30, 2024

I have not tried this package yet, have you? Your are talking about testing right (I'm not aware of native backend without objc/java yet). For testing I guess it should have to work on the workstation used (mac, linux, win) so I guess a dart2 compatible native sqlite package should work (if you know any)
An alternative for testing would be to have sqlite server and mock native calls to it. If you know any flutter package with native calls and complete unit tests, that would be great.

from sqflite.

nathansamson avatar nathansamson commented on July 30, 2024

I did not try the package yet, it seems it just wraps the native C/C++ library so it should work on mac, linux and win so should in general work for unit testing purposes

It seems the package is a bit unmainaintaned though. Not sure what its test coverage though

from sqflite.

alextekartik avatar alextekartik commented on July 30, 2024

I could not manage to make it work...

from sqflite.

jlcool avatar jlcool commented on July 30, 2024

现在还是没办法写测试吗
Is it still impossible to write tests now?

from sqflite.

alextekartik avatar alextekartik commented on July 30, 2024

There is still no simple/managed ways to run unit test on the device. In this thread #83, there is a solution though

# run the test
flutter run test/sqlite_test.dart

with some caveats...

from sqflite.

rolurq avatar rolurq commented on July 30, 2024

@alextekartik how about if you provide the mocks you used in https://github.com/tekartik/sqflite/blob/master/test/src_database_test.dart in an extra package to ease with testing?
Or maybe the same package but as an extra.
Some plugins provide mocking capabilities for testing, so is not unusual behavior.

from sqflite.

 avatar commented on July 30, 2024

I can run sqflite tests using flutter run "test file path" but I need to include test in coverage report. How should I do that. any code samples appreciated

from sqflite.

alextekartik avatar alextekartik commented on July 30, 2024

I started a doc about testing here:
#49

If anyone has already tried e2e testing or find a good way to test code with sqflite, I'd be happy to complete the documentation.

from sqflite.

maxisme avatar maxisme commented on July 30, 2024
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

  const MethodChannel('com.tekartik.sqflite')
      .setMockMethodCallHandler((MethodCall methodCall) async {
    if (methodCall.method == 'openDatabase') {
      return await databaseFactoryFfi.openDatabase(inMemoryDatabasePath);
    }
  });

Worked for me.

from sqflite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.