2013-05-24 2 views
4

Zombie.js로 node.js 코드를 테스트하고 있습니다. 나는 POST 방식에 다음과 같은 API를 가지고 :Zombie.js 브라우저를 통한 POST 메소드로 API 호출

/api/names 

내 테스트/person.js 파일에서 다음 코드 : 이제

it('Test Retreiving Names Via Browser', function(done){ 
    this.timeout(10000); 
    var url = host + "/api/names"; 
    var browser = new zombie.Browser(); 
    browser.visit(url, function(err, _browser, status){ 
     if(browser.error) 
     { 
      console.log("Invalid url!!! " + url); 
     } 
     else 
     { 
      console.log("Valid url!!!" + ". Status " + status); 
     } 
     done(); 
    }); 
}); 

, 나는에서 명령을 모카을 실행 내 단자, browser.error의 상태가됩니다. 그러나 메서드를 가져 오기 위해 API를 설정하면 예상대로 작동하고 유효한 URL (else 부분)이됩니다. 나는 이것이 post 메소드에서 API를 가지고 있기 때문이라고 생각한다.

추신 : 모바일 용 백엔드를 개발하면서 버튼 클릭에 대한 쿼리를 실행하기 위해 만든 양식이 없습니다.

POST 메소드로 API를 실행하는 방법에 대한 도움을 주시면 감사하겠습니다.

답변

1

좀비는 실제 웹 페이지와 상호 작용하고 게시물의 경우 실제 양식을 요청할 때 더 유용합니다. 당신이 requestshould 모듈을 필요 이상으로 테스트를 들면

request 모듈을 사용하여 수동으로 코드 예를 들어 POST 요청 자신

var request = require('request') 
var should = require('should') 
describe('URL names', function() { 
    it('Should give error on invalid url', function(done) { 
    // assume the following url is invalid 
    var url = 'http://localhost:5000/api/names' 
    var opts = { 
     url: url, 
     method: 'post' 
    } 
    request(opts, function (err, res, body) { 
     // you will need to customize the assertions below based on your server 

     // if server returns an actual error 
     should.exist(err) 
     // maybe you want to check the status code 
     res.statusCode.should.eql(404, 'wrong status code returned from server') 
     done() 
    }) 
    }) 

    it('Should not give error on valid url', function(done) { 
    // assume the following url is valid 
    var url = 'http://localhost:5000/api/foo' 
    var opts = { 
     url: url, 
     method: 'post' 
    } 
    request(opts, function (err, res, body) { 
     // you will need to customize the assertions below based on your server 

     // if server returns an actual error 
     should.not.exist(err) 
     // maybe you want to check the status code 
     res.statusCode.should.eql(200, 'wrong status code returned from server') 
     done() 
    }) 
    }) 
}) 

공예

npm install --save-dev request should 
관련 문제