Robolectric:未找到Shadows.shadowOf(Context)方法

时间:2018-03-27 08:50:23

标签: android robolectric

我有一个自定义系统服务,我试图模拟并添加到Robolectric 3.8中的阴影上下文。

我正在尝试按照这个答案:getSystemService in Robolectric returns object with null Context但看起来像shadowOf(android.content.Context)已被弃用但我在http://robolectric.org/migrating/中找不到它

       count, _ := r.DB.C(sellOfferCollectionName).Find(
         bson.M{
            "state":     someState,
            "asset_id":  assetId,
            "seller_id": seller,
         },
       ).Count()

       if count > 0 {
           return 
       }       

       id := uuid.New().String()
            OfferModel := Offer{
                Id:           id,
                AssetId:      assetId,
                State:        someState,
                SellerId:     sellerId,
                CreatingDate: time.Now(),
            }
        if _, err := r.DB.C(sellOfferCollectionName).UpsertId(offer.Id, offer); err != nil {
            return err
        }

如何在Robolectric 3.8中模拟/隐藏系统服务?

1 个答案:

答案 0 :(得分:2)

ShadowContextImpl shadowContext = Shadow.extract(RuntimeEnvironment.application.getBaseContext());
shadowContext.setSystemService(key, service);
相关问题