在我的案例中如何用sinon单位测试承诺?

时间:2017-03-27 08:15:34

标签: node.js unit-testing sinon

我正在尝试在我的应用程序的单元测试中启动存根。

我的文件中有这样的内容。

assert.equal error

我的问题是我似乎无法触发test。运行测试时,我可以在输出中看到“foo”。但是,测试应该失败,因为该值应为not foooo而不是“ XXXfragment extends customFragment { GridView gridView; GridAdapter adapter; ArrayList<GridItem> items = new ArrayList<>(); @Override public void onCreate(){ // someInitialize Coded adapter = new ArrayAdapter<GridItem>(this,items); gridView.setAdapter(adapter); } @Override public void onActivityCreated(){ getImagesList(); // some network process } @Override public void responseData(){ super(); items.clear(); items.addAll( getImageURLItems() ); setGridViewHeight( gridView ); // set gridView because it inside in ScrollView adapter.notifydatasetchanged();// here is problem occured } public void setGridViewHeight(GridView gridView){ gridView.post(new Runnable(){ // initialize items for gridView }); } } public class GridAdapter extends ArrayAdapter<GridItem>{ @Override public void getView( public View getView(final int position, View convertView, ViewGroup parent ) { ViewHolder listImageHolder; AQuery aq = new AQuery( getContext() ); final GridItem item = getItem( position ); View v = convertView; if ( v == null || v.getId() == R.id.loPhotoAdd ) { v = LayoutInflater.from( getContext() ).inflate( R.layout.grid_item_photo_in_ex, null ); } listImageHolder = new ViewHolder(); listImageHolder.imageView = (ImageView) v.findViewById( R.id.img ); v.setTag(listImageHolder); }else { listImageHolder = (ViewHolder)v.getTag(); } if ( item != null ) { TextView text = (TextView) v.findViewById( R.id.text ); text.setText( item.title ); if(item.filePath != null) { final int HashCode = listImageHolder.imageView.hashCode(); Log.d(this.getClass().getSimpleName(),"position"+position); Log.d(this.getClass().getSimpleName(),"hashcode"+HashCode); BitmapAjaxCallback callback = new BitmapAjaxCallback(); callback.rotate(true); aq.id(listImageHolder.imageView).image(item.filePath,true,true,200,1,callback).clicked(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "itemCateCode : "+ item.categoryCode+"\n"+ "filePath : "+ item.filePath+"\n"+ "itemCode : "+ item.itemCode+"\n"+ "idx : "+ item.idx+"\n"+ "title : "+ item.title+"\n"+ "pos : "+position +"\n"+ "hashCode :"+HashCode , Toast.LENGTH_SHORT).show(); } });; } return v; } } ”。由于某种原因它通过了。我不确定原因。有人可以帮我吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

你需要返回承诺,以便摩卡等待:

describe('test unit Tests', function(){
    describe('My first test', function(){
        it('should test a promise', function(){
            return sinon.stub().resolves('foo')().then(function (value) {
              console.log('test');

              assert.equal(value, 'not foooo')
            })
        })
    })
});