我如何用摩卡测试我的快速应用程序? [英] How do i test my express app with mocha?

查看:142
本文介绍了我如何用摩卡测试我的快速应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将应用程序和mocha添加到我的快速应用程序进行测试,但我想知道如何测试我的应用程序。我想这样做:

I've just added shouldjs and mocha to my express app for testing, but I'm wondering how to test my application. I would like to do it like this:

app = require '../app'
routes = require '../src/routes'

describe 'routes', ->
  describe '#show_create_user_screen', ->
    it 'should be a function', ->
      routes.show_create_user_screen.should.be.a.function
    it 'should return something cool', ->
      routes.show_create_user_screen().should.be.an.object

当然,该测试套件的最后一次测试只是告诉med,res.render函数(在show_create_user_screen中调用)未定义,可能是因为服务器未运行,并且配置尚未完成。所以我想知道其他人如何设置他们的测试?

Of course, the last test in that test-suite just tells med that the res.render function (called within show_create_user_screen) is undefined, probably becouse the server is not running and the config has not been done. So I wonder how other people set up their tests?

推荐答案

好的,首先虽然测试你的路由代码是你可能或可能一般来说,一般来说,尝试将纯粹的JavaScript代码(类或函数)中的有趣的业务逻辑与快速或任何您正在使用的框架分离,并使用香草摩卡测试来测试。一旦你实现了,如果你想真正测试你在摩卡配置的路线,你需要传递mock req,res 参数到你的中间件功能模仿之间的接口快递/连接和您的中间件。

OK, first although testing your routing code is something you may or may not want to do, in general, try to separate your interesting business logic in pure javascript code (classes or functions) that are decoupled from express or whatever framework you are using and use vanilla mocha tests to test that. Once you've achieved that if you want to really test the routes you configure in mocha, you need to pass mock req, res parameters into your middleware functions to mimic the interface between express/connect and your middleware.

对于一个简单的情况,您可以创建一个模拟 res code> render 函数看起来像这样。

For a simple case, you could create a mock res object with a render function that looks something like this.

describe 'routes', ->
  describe '#show_create_user_screen', ->
    it 'should be a function', ->
      routes.show_create_user_screen.should.be.a.function
    it 'should return something cool', ->
      mockReq = null
      mockRes =
        render: (viewName) ->
          viewName.should.exist
          viewName.should.match /createuser/

      routes.show_create_user_screen(mockReq, mockRes).should.be.an.object

还只是FYI中间件函数不需要返回任何特定的值,这是他们使用 req,res,next 您应该在测试中关注的参数。

Also just FYI middleware functions don't need to return any particular value, it's what they do with the req, res, next parameters that you should focus on in testing.

这是您在评论中请求的一些JavaScript。 p>

Here is some JavaScript as you requested in the comments.

describe('routes', function() {
    describe('#show_create_user_screen', function() {
      it('should be a function', function() {
        routes.show_create_user_screen.should.be.a["function"];
      });
      it('should return something cool', function() {
        var mockReq = null;
        var mockRes = {
          render: function(viewName) {
            viewName.should.exist;
            viewName.should.match(/createuser/);
          }
        };
        routes.show_create_user_screen(mockReq, mockRes);
      });
    });
  });

这篇关于我如何用摩卡测试我的快速应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆