English 中文(简体)
如何检验我对暴风?的直言?
原标题:How do i test my express app with mocha?

我刚才在我的试验口号中添加了 should和 mo,但我很想知道如何测试我的应用。 我愿这样做:

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

当然,这次试卷中的最后一次测试仅仅告诉人们,转录功能(在显示_create_user_ Screen)没有界定,可能不会使服务器运行,而且没有这样做。 因此,我想知道其他人如何进行测试?

最佳回答

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,标有render/code>功能,看上类似情况。

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

而且,光辉电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层,电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层电离层。

正如你在评论中要求的那样,这里是 Java。

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);
      });
    });
  });
问题回答

您可以尝试超级试验,然后,服务器的开端和停机处理:

var request = require( supertest )
  , app     = require( ./anExpressServer ).app
  , assert  = require("assert");

describe( POST / , function(){
  it( should fail bad img_uri , function(done){
    request(app)
        .post( / )
        .send({
             img_uri  :  foobar 
        })
        .expect(500)
        .end(function(err, res){
            done();
        })
  })
});

以前、之前、之前、之后和之后都发生了 mo子检测。 在此情况下,您在发言前应使用。

describe  routes  ->
  before (done) ->
    app.listen(3000)
    app.on( connection , done)

I ve found it s easiest to set up a TestServer class to be used as a helper, as well as a helper http client, and just make real requests to a real http server. There may be cases where you want to mock and stub this stuff instead though.

// Test file
var http = require( the/below/code );

describe( my_controller , function() {
    var server;

    before(function() {
        var router = require( path/to/some/router );
        server = http.server.create(router);
        server.start();
    });

    after(function() {
        server.stop();
    });

    describe("GET /foo", function() {
        it( returns something , function(done) {
            http.client.get( /foo , function(err, res) {
                // assertions
                done();
            });
        });
    });
});


// Test helper file
var express    = require( express );
var http       = require( http );

// These could be args passed into TestServer, or settings from somewhere.
var TEST_HOST  =  localhost ;
var TEST_PORT  = 9876;

function TestServer(args) {
    var self = this;
    var express = require( express );
    self.router = args.router;
    self.server = express.createServer();
    self.server.use(express.bodyParser());
    self.server.use(self.router);
}

TestServer.prototype.start = function() {
    var self = this;
    if (self.server) {
        self.server.listen(TEST_PORT, TEST_HOST);
    } else {
        throw new Error( Server not found );
    }
};

TestServer.prototype.stop = function() {
    var self = this;
    self.server.close();
};

// you would likely want this in another file, and include similar 
// functions for post, put, delete, etc.
function http_get(host, port, url, cb) {
    var options = {
        host: host,
        port: port,
        path: url,
        method:  GET 
    };
    var ret = false;
    var req = http.request(options, function(res) {
        var buffer =   ;
        res.on( data , function(data) {
            buffer += data;
        });
        res.on( end ,function(){
            cb(null,buffer);
        });
    });
    req.end();
    req.on( error , function(e) {
        if (!ret) {
            cb(e, null);
        }
    });
}

var client = {
    get: function(url, cb) {
        http_get(TEST_HOST, TEST_PORT, url, cb);
    }
};

var http = {
    server: {
        create: function(router) {
            return new TestServer({router: router});
        }
    },

    client: client
};
module.exports = http;




相关问题
MochaUI tutorial [closed]

where can I find a good and complete tutorial for MoachaUI? I know good CSS, basic JS too and ofcourse php! but this UI stole my heart but I couldn t find a good tutorial to learn it :( Thanks

Testing association methods with Mocha

I have a Rails app with an Order and a Refund model. Order has_many :refunds. All well and good. I m trying to write a functional test for refund logic in a controller. Here s what I have right now: ...

Mocking with rspec and mocha together

For a test suite that s already using mocha for mocking, can new tests be written with rspec mocking? maybe turn that on before(:all) and turn it back to mocha after(:all) I tried changing the Spec::...

How do you test an AJAX request with RSpec/RoR?

I m fairly new to RoR and recently started learning BDD/Rspec for testing my application. I ve been looking for a way to spec an AJAX request, but so far I haven t found much documentation on this at ...

How are both of these tests passing?

I m using Shoulda, Mocha, and Test::Unit for testing. I have the following controller and test: class FoosController < ApplicationController caches_action :show def show @foo = ...

Mocking authlogic from shoulda with mocha

I am having trouble mocking authlogic from shoulda. I have the following test fixture: class HomeControllerTest < ActionController::TestCase context "on GET to index" do setup do ...

Rails Faking a Route

To be specific, I m trying to get ActionController::Routing::Routes.recognize_path to recognize a route that is not in routes.rb, for testing purposes. Is it possible to somehow mock or dynamically ...

Does mocha run the code in a stub (Rails)?

Im new to tdd and stubbing. When I stub a method im assumng that any code within that method does not get executed? Im trying to fake the method raising an exception but the results of my test ...