用协程测试注入的物体

时间:2019-07-07 17:36:32

标签: android unit-testing kotlin-coroutines

我的演示者有一个注入的空对象。当我运行用例(带有协程)时,我想检查对象的包含,但始终为空。

我的用例只是获取列表对象并修改一些参数。

class CheckoutPresenter(
    private val view: CheckoutView?,
    private val getProductsFromCartUseCase: GetProductsFromCartUseCase,
    private val getTotalPriceUseCase: GetTotalPriceUseCase,
    private var products: MutableList<Product>
) {

 fun getTotalPrice() {
        getTotalPriceUseCase.setData(products)
        getTotalPriceUseCase.execute(this::onSuccessTotalPriceProducts, this::onFailProducts)
    }

private fun onSuccessTotalPriceProducts(products: MutableList<Product>) {
        this.products = products
        ...
}

我的测试

@Test
fun ` example`() = testDispatcher.runBlockingTest {
    MainScope().launch {
        //Given
        products = ProductsMotherObject.createProductModel().values.toMutableList()
        presenter = CheckoutPresenter(view, getProductsFromCartUseCase, getTotalPriceUseCase, products)

        //when
        presenter.getTotalPrice()

        //then
        products //when I debug doesn't contain the information.
    }
}

我的产品对象测试仍然为空

1 个答案:

答案 0 :(得分:3)

我对public class MainActivity extends AppCompatActivity { Intent msgIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); msgIntent = new Intent(MainActivity.this, MiIntentService.class); msgIntent.putExtra("iteraciones", 10); startService(msgIntent); //registrando nuestro broadcast a la aplicacion y filtrando solo para las acciones especificadas IntentFilter filtroBroadcast = new IntentFilter(); filtroBroadcast.addAction(MiIntentService.ACTION_FIN); filtroBroadcast.addAction(MiIntentService.ACTION_PROGRESO); ProgressReceiver progressReceiver = new ProgressReceiver(); registerReceiver(progressReceiver,filtroBroadcast); } public class ProgressReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(MiIntentService.ACTION_PROGRESO)){ Toast.makeText(getApplicationContext(),String.valueOf(intent.getIntExtra("progreso",0)),Toast.LENGTH_LONG).show(); } if(intent.getAction().equals(MiIntentService.ACTION_FIN)){ Toast.makeText(getApplicationContext(),"Se termino de realizar las 10 tareas que se tenia",Toast.LENGTH_LONG).show(); } } } } 为空的猜测,因为您没有在其中添加任何元素。您的回叫在这里:

int MaxSum(vector<int>&A) {
    int n=0,m=0;
    int max_so_far=0,max_ending_here=0;
    int prevIndex = 0;
    for(int i=0;i<A.size();i++) {
        max_ending_here += A[i];
        if(max_ending_here > max_so_far) {
            max_so_far = max_ending_here;
            n = prevIndex;
            m = i;
        }
        if(max_ending_here < 0) {
            max_ending_here = 0;
            prevIndex=i+1;
            prevIndex=i+1;
        }
    }

    cout<<"n: "<<n<<endl;
    cout<<"m: "<<m<<endl;
}

不会修改您通过构造函数传递的列表。它只是将引用更改为一个全新的列表,您的测试无法访问该列表。

将元素从products(方法参数)添加到private fun onSuccessTotalPriceProducts(products: MutableList<Product>) { this.products = products ... } (域)中,而不只是重新分配字段:

products