如何告诉Jasmine建立/清理所有API请求

时间:2016-11-30 14:29:56

标签: jasmine promise generator

我正在写e2e测试(Protractor,Jasmine) 我必须多次调用API来创建,测试,然后删除资源(分页测试)。 我使用了request-promise + co方法(参见下面的要点)。 但是,我的Jasmine Test需要知道所有API请求何时完成。 返回指示API运行的方式/内容是什么?

//Jasmine test // AUT = app under test // HCS = some resouce to be created multiple times to test pagination
// defaultData has 11 defaultHCSID in array

describe ("Test: Create 11 HCSes to test pagination", function () {
    beforeAll(function (done) {
        AUT.api.defaultHCS.create(defaultData).then(()=> {
            done(); 
            //setTimeout(function () {done();}, AUT.API_TIMEOUT);
        });
    });
..
rest of the test.
..

//appointments defaultHCS Api.js
var co = require('co');
var _ = require('lodash');
var rp = require('request-promise');
var urlLogin ={url:"<URL>", body:"<body>" method: 'POST', headers: {"Content-Type": "application/json"}, json: true}
var urlDefaultHCSSave ={url:"<URL>", body:"<body>" method: 'POST', headers: {"Content-Type": "application/json"}, json: true}

exports.create = function (customTestData) {
    return co(function*() {
        var response, token, testData;
        testData = customTestData ||  defaultTestData;
        response = yield rp(urlLogin);
          urlDefaultHCSSave.headers["x-auth-token"] =   response.token;

        var size= _.size(testData.defaultHCSID);
        process.stdout.write("API DEFAULT HCS Create. Size: " + size + " ");
        var total = 0;
        testData.defaultHCSID.forEach(function (item,i){
            return co(function* () {
                response = yield rp(defaultHCSSave); //parsedBody
                total+=1;
                if (total === size){  process.stdout.write("All Done\n");      }
            }).catch(function (err) {console.error(err.stack); })
        });
    });
};

1 个答案:

答案 0 :(得分:0)

package com.example.ericallenbellville.rcbeamdesign; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { public Button btnCalc; private TextView tvaResult; private TextView tvcResult; private TextView tvetResult; private TextView tvphiResult; private TextView tvMnResult; private TextView tvphiMnResult; private TextView tvbeta1Result; private EditText etB,etD,etAs,etFc,etFy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { btnCalc = (Button)findViewById(R.id.btnCalc); etB = (EditText)findViewById(R.id.etB); etD = (EditText)findViewById(R.id.etD); etAs = (EditText)findViewById(R.id.etAs); etFc = (EditText)findViewById(R.id.etFc); etFy = (EditText)findViewById(R.id.etFy); tvaResult = (TextView)findViewById(R.id.tvaResult); tvcResult = (TextView)findViewById(R.id.tvcResult); tvetResult = (TextView)findViewById(R.id.tvetResult); tvphiResult = (TextView)findViewById(R.id.tvphiResult); tvMnResult = (TextView)findViewById(R.id.tvMnResult); tvphiMnResult = (TextView)findViewById(R.id.tvphiMnResult); tvbeta1Result = (TextView)findViewById(R.id.tvbeta1Result); btnCalc.setOnClickListener(this); } @Override public void onClick(View view) { Double B = Double.parseDouble(etB.getText().toString()); Double D = Double.parseDouble(etD.getText().toString()); Double As = Double.parseDouble(etAs.getText().toString()); Double Fc = Double.parseDouble(etFc.getText().toString()); Double Fy = Double.parseDouble(etFy.getText().toString()); Double aResult = Double.parseDouble(tvaResult.getText().toString()); Double cResult = Double.parseDouble(tvcResult.getText().toString()); Double etResult = Double.parseDouble(tvetResult.getText().toString()); Double beta1Result = Double.parseDouble(tvbeta1Result.getText().toString()); Double phiResult = Double.parseDouble(tvphiResult.getText().toString()); Double MnResult = Double.parseDouble(tvMnResult.getText().toString()); Double phiMnResult = Double.parseDouble(tvphiMnResult.getText().toString()); switch(view.getId()) { case R.id.btnCalc: if (Fc <= 4000) { beta1Result = (0.85); } else if (4000 < Fc && Fc <= 8000) { beta1Result = ((0.85)-(0.05 * ((Fc - 4000) / (1000)))); } else { beta1Result = 0.65; } aResult = ((Fy * As) / (0.85 * Fc * B)); cResult = (aResult / beta1Result); etResult = (((D - cResult) / (cResult)) * 0.003); if (etResult >= 0.005) { phiResult = (0.9); } else if (0.002 <= etResult && etResult < 0.005) { phiResult = (0.65 + (etResult - 0.002) * 0.25 / (0.005 - 0.002)); } else { phiResult = (0.00); } MnResult = (((Fy * As) * (D - (aResult / 2.0)))); phiMnResult = phiResult * MnResult; tvaResult.setText(String.valueOf(aResult)); tvcResult.setText(String.valueOf(cResult)); tvetResult.setText(String.valueOf(etResult)); tvphiResult.setText(String.valueOf(phiResult)); tvMnResult.setText(String.valueOf(MnResult)); tvphiMnResult.setText(String.valueOf(phiMnResult)); break; }} } 是非阻塞的,异步的,替换为 &#39; for..of`工作。 (阻止,同步)