在Robolectric测试SQLite数据库数据库、测试、Robolectric、SQLite

2023-09-05 07:50:40 作者:恰好心动

我想测试我的Andr​​oid应用程序中使用Robolectric一个简单的SQLite数据库。我把一些价值,但阅读时回0行被返回。

I'm trying to test a simple SQLite database using Robolectric in my Android application. I'm putting in some values, but when reading them back 0 rows are returned.

我使用SQLiteOpenHelper类来访问数据库。

I'm using the SQLiteOpenHelper class to access the database.

// RequestCache extends SQLiteOpenHelper
RequestCache cache = new RequestCache(activity); 
SQLiteDatabase db = cache.getWritableDatabase();

// Write to DB
ContentValues values = new ContentValues();
values.put(REQUEST_TIMESTAMP, TEST_TIME); 
values.put(REQUEST_URL, TEST_URL);
db.insertOrThrow(TABLE_NAME, null, values);

// Read from DB and compare values      
Vector<Request> matchingRequests = new Vector<Request>();
db = cache.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, SEARCH_URL_RETURN_COLUMNS, SEARCH_URL_WHERE, new String[] {url}, null, null, ORDER_BY, null);
int id = 0;

while(cursor.moveToNext()) {
    long timestamp = cursor.getLong(0);
    Request request = new Request(id++);
    request.setUrl(url);
    request.setCreationTimestamp(new Date(timestamp));
    matchingRequests.add(request);
}


// Assert that one row is returned
assertThat(matchingRequests.size(), equalTo(1));  // fails, size() returns 0

在调试robolectric这个工程预期​​外的code。难道我做错什么或者是不是可以测试使用Robolectric SQLite数据库?

When debugging the code outside robolectric this works as expected. Am I doing anything wrong or is it not possible to test SQlite databases using Robolectric?

推荐答案

的问题是,Robolectric的SQLiteDatabase只存储在 记忆,所以当你调用getReadableDatabase或getWritableDatabase, 现有数据库将与新的空数据库被覆盖。

NOTE: This answer is outdated. If you are using Roboletric 2.X, please see http://stackoverflow.com/a/24578332/850787

The problem is that Robolectric's SQLiteDatabase is stored only in memory, so when you call getReadableDatabase or getWritableDatabase, the existing database will be overridden with new empty database.

我跑了同样的问题,唯一的解决办法,我发现 是,我需要用叉子叉Robolectric项目和补充 ShadowSQLiteOpenHelper保存数据库,如果同样的情况下被提供两个 次。然而,随着我的叉子的问题是,我不得不禁用 关闭() - 函数时,CONTEX给出,否则 connection.close()时会破坏内存中的数据库。我已经拉请求,但它不合并到项目呢。

I was running to the same problem and only solution I found was that I needed to fork the Robolectric project and added ShadowSQLiteOpenHelper to save database if same context is given two times. However the problem with my fork is that I had to 'disable' close()-function when contex is given because otherwise Connection.close() will destroy the database in memory. I have made pull request for it but it isn't merged to project yet.

但随意复制我的版本,它应该解决您的问题(如果我 理解正确的话:P)。 它可以在GitHub上找到: https://github.com/waltsu/robolectric

But feel free to clone my version and it should fix your problem (If I understood it correctly :P ). It can be found on GitHub: https://github.com/waltsu/robolectric

下面是一个例子,如何用修改:

Here is a example how to use the modification:

Context c = new Activity(); 
SQLiteOpenHelper helper = new SQLiteOpenHelper(c, "path", null, 1); 
SQLiteDatabase db = helper.getWritableDatabase(); 
// With the db write something to the database 
db.query(...); 
SQLiteOpenHelper helper2 = new SQLiteOpenHelper(c, "path", null, 1); 
SQLitedatabase db2 = helper2.getWritableDatabase(); 
// Now db and db2 is actually the same instance 
Cursor c = db2.query(...) ; // Fetch the data which was saved before 

Ofcourse,你不需要创造新的SQLiteOpenHelper,但是这仅仅是例子,传递相同的情况下,以两个不同的SQLiteOpenHelper将给予相同的数据库。

Ofcourse you don't need to create new SQLiteOpenHelper, but that is just example that passing same context to two different SQLiteOpenHelper will give same database.