如何在node-tap中使用beforeEach?

时间:2016-07-11 02:54:08

标签: javascript node.js unit-testing tap

有人可以举例说明如何使用df.loc[df.Dates == "2011-01-01", "Category"].value_counts().sort_values()[::-1][:10] 吗? http://www.node-tap.org/api/ 理想情况下,promise版本的示例,但回调版本示例也不错。

这是我创建的测试工作正常:

beforeEach

在我做任何事之前,我想坚持'use strict'; const t = require('tap'); const tp = require('tapromise'); const app = require('../../../server/server'); const Team = app.models.Team; t.test('crupdate', t => { t = tp(t); const existingId = '123'; const existingData = {externalId: existingId, botId: 'b123'}; const existingTeam = Team.create(existingData); return existingTeam.then(() => { stubCreate(); const newId = 'not 123' const newData = {externalId: newId, whatever: 'value'}; const newResult = Team.crupdate({externalId: newId}, newData); const existingResult = Team.crupdate({externalId: existingId}, existingData); return Promise.all([ t.equal(newResult, newData, 'Creates new Team when the external ID is different'), t.match(existingResult, existingTeam, 'Finds existing Team when the external ID exists') ]); }); }) .then(() => { process.exit(); }) .catch(t.threw); function stubCreate() { Team.create = data => Promise.resolve(data); } 。保存后,我想要存根existingTeam。在这两件事之后,我想开始实际测试。我认为如果不使用Team.create或者可能重复测试代码,我可以使用Promise.all

我如何将其转换为使用beforeEach?或者它的用法是什么?

1 个答案:

答案 0 :(得分:2)

简单,只需从回调函数返回承诺

public class SpinnerCursorAdapter extends CursorAdapter {

public SpinnerCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = LayoutInflater.from(context).inflate(R.layout.spinner_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    view.setTag(viewHolder);
    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder viewHolder = (ViewHolder) view.getTag();

    int flagIconId = Integer.valueOf(cursor.getString(5));
    viewHolder.flagIcon.setImageResource(flagIconId);

    String currencyName = cursor.getString(4);
    viewHolder.currencyName.setText(currencyName);

    viewHolder.currencyCharCode.setText(cursor.getString(1));
}

private static class ViewHolder {
    final ImageView flagIcon;
    final TextView currencyName;
    final TextView currencyCharCode;

    private ViewHolder(View view) {
        flagIcon = (ImageView) view.findViewById(R.id.spinner_flag_icon);
        currencyName = (TextView) view.findViewById(R.id.spinner_currency_name);
        currencyCharCode = (TextView) view.findViewById(R.id.spinner_currency_char_code);
    }
}