我正在为 a 编写测试ContentProvider
,在insert
我通知更改时getContext().getContentResolver().notifyChange(mUri, null);
我的测试类扩展了ProviderTestCase2
。我创建了以下模拟 ContentObserver 类:
private class ContentObserverMock extends ContentObserver {
public boolean changed = false;
public ContentObserverMock(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}
@Override
public void onChange(boolean selfChange) {
changed = true;
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
}
这是测试用例:
public void testInsertNotifyContentChanges() {
ContentResolver resolver = mContext.getContentResolver();
ContentObserverMock co = new ContentObserverMock(null);
resolver.registerContentObserver(CONTENT_URI, true, co);
ContentValues values = new ContentValues();
values.put(COLUMN_TAG_ID, 1);
values.put(COLUMN_TAG_CONTENT, "TEST");
resolver.insert(CONTENT_URI, values);
assertTrue(co.changed);
}
似乎onChange
从未调用过,我也尝试ContentObserverMock co = new ContentObserverMock(new Handler());
过相同的结果。
我在这里做错了什么?