【问题标题】:Node.js with mocha test带有 mocha 测试的 Node.js
【发布时间】:2014-09-17 09:17:40
【问题描述】:

如果我有如下功能:

function foo(request,response){
  var val = request.param('data');
  //code here
});

我怎样才能为此创建一个 mocha 测试函数来传递请求和响应参数。

【问题讨论】:

    标签: javascript node.js mocha.js


    【解决方案1】:

    您在上面编写的函数可以看作是一个控制器 - 它处理请求并返回响应。

    您可以做的事情很少:

    1. 您可以测试路由本身 - 向使用此控制器的端点发出 http 请求并测试其行为是否正确 - 例如,您可以使用 request/supertest/superagent 库。

    2. 您可以模拟请求和响应对象并直接测试代码 - 它不需要启动服务器,但您需要花一些时间来正确模拟对象。

    【讨论】:

      【解决方案2】:

      这取决于你的“代码”做什么,以及你想做什么:

      测试逻辑

      如果您可以在接受“val”的方法中分离代码并返回结果,那么只需对其进行测试。 通常,从请求中获取参数并将结果传递给响应是不费吹灰之力的,不值得测试。

      foo : function (req, res) {
      
        // Do you really need to test that ?
        var data = req.param("data");
      
        // You probably want to test that      
        var bar = doFooLogic(data);
      
        // Do you really need to test that ?
        res.json(bar);
      
      },
      
      doFooLogic : function (data) {
          ...
      }
      

      还有一个类似的测试:

      describe("foo's logic", function () {
      
        it("does stuff", function () {
           // Just test the logic.
           // This assumes you exposed the doFooLogic, which is probably acceptable
           var bar = doFooLogic(42);
           assert(bar.xxxx); // Whatever
        });
      
      });
      

      测试req/response函数:

      如果你真的想要,如果你只是在请求对象上使用“param”,你也许可以轻松地模拟请求/响应(这是 JS,你只需要传递具有相同功能的东西可用):

      describe(..., function () {
      
        it("does whatever", function () {
      
          var mockRequest = {
            param : function (key) {
               if (key === "data") {
                 return 42;
               } else {
                 throw new Error("UNexpected key", key)
               }
            }
          }
      
          var mockResponse = {
            // mock whatever function you need here 
            json : function (whatever) {
                 assert(whatever.xxxx) // compare what you put into the response, for example
            }
          }
      
          // Then do the call
          foo (mockRequest, mockResponse);
      
          // The hard part is then how to test the response was passed the right stuff.
          // That's why testing the logic is probably easier.
      

      【讨论】:

        【解决方案3】:

        我认为您可以简单地用 Sinon.js 之类的东西来模拟它。应该是这样的:

        describe('...', function( done ){
         it('should test something', function(done){
           var mock = sinon.stub(request, "param").withArgs("data").returns("Whatever");
        
           var val = request.param('data');
        
           //do your logic with that value
        
           assert.equal(/*whatever value you want check*/);
        
           mock.restore();
        
           done();
         }
        }
        

        而且您不必关心请求的内容。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-23
          • 2017-10-20
          • 1970-01-01
          • 2017-08-03
          相关资源
          最近更新 更多