使用Javascript测试的初学者。我正在使用Mocha,但路易斯明智地说,这个问题并不是专门针对Mocha的。我有一个Node应用程序,它有一些页面对匿名用户可见,如果你没有登录,你应该看不到一些页面。因此,作为一个非常简单的开始,
describe('User Access', function(){
it('should allow anyone to access the help desk (about page)', function(done){
request(host)
.get('/')
.expect(200, done);
}),
it('should allow anyone to access the contact page', function(done){
request(host)
.get('/contact')
.expect(200, done);
}),
//initially we were expecting 404, we need anything BUT 200.
it('should NOT allow anonymous user to access the Training Material page', function(done){
request(host)
.get('/training')
.expect(404, done);
}),等
最初,这是可行的。但是,开发人员已将不可用页面更改为302状态,并将这些页面重定向到web应用的根目录。因此,为了让开发人员灵活地实现这一限制,我想将其更改为否定断言。那么,使用Mocha语法,我如何“期望”响应不是200呢?
发布于 2014-01-29 04:33:59
docs here说,您可以传入一个自定义断言函数,该函数被赋予响应对象,从该函数返回一个值将意味着断言失败,即;
.expect(function(res){
if(res.status == 200){
return "we dont like 200!";
}
})https://stackoverflow.com/questions/21414677
复制相似问题