2015-07-04 42 views
0

我試圖使用supertest通過一個REST端點返回的響應包含moongose驗證的驗證錯誤,它看起來像下面如何聲明使用supertest的貓鼬驗證錯誤?

errors: Object { 
    firstName: Object { 
    kind: 'required', 
    message: 'Path `firstName` is required.', 
    name: 'ValidatorError', 
    path: 'firstName', 
    properties: Object { 
     message: 'Path `{PATH}` is required.', 
     path: 'firstName', 
     type: 'required' 
    }, 
    } 

我寫下面的測試

it('should return well formatted error response when first name is missing', function(done){ 
    var user = {lastName:"Ranger", email:"[email protected]"}; 
    request(app) 
    .post('/api/user') 
    .send(user) 
    .end(function(err, res){ 
     res.body.should.have.property("path", "firstName"); 
     done(); 
    }); 
}); 

但我驗證收到以下錯誤

AssertionError: expected Object { 
errors: Object { 
    firstName: Object { 
    kind: 'required', 
    message: 'Path `firstName` is required.', 
    name: 'ValidatorError', 
    path: 'firstName', 
    properties: Object { 
     message: 'Path `{PATH}` is required.', 
     path: 'firstName', 
     type: 'required' 
    }, 
    } 
}, 
message: 'User validation failed', 
name: 'ValidationError', 
} to have property path 
    at Test.<anonymous> (test/userTests.js:23:25) 
    at net.js:1276:10 

我怎麼能寫這樣的斷言?

回答

0

你聲稱res.body擁有財產'path',但是res.body是父對象僅包含屬性errorsmessagename

您可以測試這些屬性,也可以按如下方式訪問嵌入錯誤對象:

.end(function(err, res){ 
    res.body.errors.firstName.should.have.property("path") 
    done(); 
}); 

很可能更方便只是爲了測試父對象的屬性,因爲這應該是充足。