1

I wanna do a TDD. However, I am going to write test on my controller function for my sails.js project

/*---------------------
    :: Gamble
    -> controller
---------------------*/
var GambleController = {

  index: function(req, res) {
      res.send('Hello World!');
  }


};
module.exports = GambleController;

However , how can i write a test to test index function that output Hello world? Any one can give a example? Thanks

4

2 回答 2

5

您可以使用superagent,有一些示例用法,这是一个

describe('/signout', function() {
  var agent = superagent.agent();
  it('should start with signin', loginUser(agent));
  it('should sign the user out', function(done) {
    agent.get('http://localhost:3000/signout').end(function(err, res) {
      res.should.have.status(200);
      res.redirects.should.eql(['http://localhost:3000/']);
      res.text.should.include('Who are you?');
      return done();
    });
  });
  // ...
于 2013-07-16T17:02:35.240 回答
1

存根结果对象应该非常简单:

describe('when we get the game controller', function () {
   it ('should return hello world', function (done) {
      GameController.index(null, {
         send: function (message) {
            assert.equal(message, 'Hello World!');
            done();
         };
      });
   });
});
于 2014-03-26T14:01:13.103 回答