assert不能正常工作?
发布于 9 年前 作者 aaronluo 3553 次浏览 最后一次编辑是 8 年前 来自 问答

最近在学习Mocha写了个简单的测试,但是就被打败了,assert不能正常工作,代码如下

		describe('The code attribute', function(){
			it('should limited as 3 bits alphets', function(){
				var harbor = Harbor.build({name: 'Xinggang', 
					code: 'AB', harbor_type: 'SOC'});
				harbor.save().then(function(dataVal){
					assert.equal(dataVal, null);
				},
				function(err){
					assert.notEqual(dataVal, null);
				});
			});
		});
	});

我希望这个测试fail掉,但是控制台显示如下异常, 感觉assert模块没有工作,请帮忙解答 Possibly unhandled AssertionError: {"id":1,"name":"Xinggang","code":"AB","harbor_type":"SOC","updatedAt":"2014-11-17T08:44:01.000Z","createdAt":"2014-11-17T08:44:0 == null

2 回复

控制台的意思是dataVal不是null, 因此和null不等。这是因为你的save方法成功保存数据,然后调用function(dataVal)这个回调。 还有,你这样写有问题的。

 describe('The code attribute', function(){
            it('should limited as 3 bits alphets', function(){
                var harbor = Harbor.build({name: 'Xinggang', 
                    code: 'AB', harbor_type: 'SOC'});
                harbor.save().then(function(dataVal){
                    assert.notEqual(dataVal, null);
                },
                function(err){
                    assert.equal(err, null);
                });
            });
        });
    });

这样,保存成功这个测试就过了,不成功就报错。

@eeandrew 如果我想保存成功这个测试失败该如何写?

回到顶部