2014-09-20 168 views
0

以下工作agent.auth描述塊

describe('My App', function() { 
    describe('when logged in', function() { 
    it('should allow registered user to make a thing', function(done) { 
     agent.post('/make-a-thing') 
     .auth('testusername', 'validuserpass') 
     .send({thingName:'mythingname'}) 
     .expect(201) 
     .end(function(err, res) { 
     if (err) return done(err); 
     res.body.should.have.property('thingUrl').and.to.match(/thing\/[0-9a-f]+$/); 
     done(); 
     }); 
    }); 
    }); 
}); 

現在,如果我想越來越多的測試增加了塊「登錄時」,我不知道想每次重複.auth('testusername', 'validuserpass')行。我應該把認證代碼放在beforeEach,因爲那是beforeEach的原因。

所以,我想這一點:

describe("My App", function() { 

    describe('when logged out', function() { 
    it('should disallow anonymous user from doing things', function(done) { 
     agent.post('/do-things') 
     .send({thingName:'mythingname'}) 
     .expect(403) 
     .end(function(err, res) { 
     if (err) return done(err); 
     done(); 
     }); 
    }); 
    }); 

    describe('when invalid user', function() { 
    beforeEach(function(done) { 
     agent.auth('invalidusername', 'invaliduserpass'); 
     done(); 
    }); 

    it('should disallow unrecognized user from doing things', function(done) { 
     agent.post('/do-things') 
     .send({thingName:'mythingname'}) 
     .expect(403) 
     .end(function(err, res) { 
     if (err) return done(err); 
     done(); 
     }); 
    }); 
    }) 

    describe('when logged in', function() { 
    beforeEach(function(done) { 
     agent.auth('testusername', 'validuserpass'); 
     done(); 
    }); 

    it('should allow registered user to make a thing', function(done) { 
     agent.post('/make-a-thing') 
     .send({thingName:'mythingname'}) 
     .expect(201) 
     .end(function(err, res) { 
     if (err) return done(err); 
     res.body.should.have.property('thingUrl').and.to.match(/thing\/[0-9a-f]+$/); 
     done(); 
     }); 
    }); 

    it('should require name attribute to create a thing', function(done) { 
     agent.post('/make-a-thing') 
     .send({notaname:'notathingname'}) 
     .expect(409) 
     .expect('Content-Type', /json/) 
     .end(function(err, res) { 
     if (err) return done(err); 
     done(); 
     }); 
    }); 
    }); 

}); 

發生的事情是沒有定義agent.auth。我認爲auth方法是在auth.post的結果中定義的。

有沒有辦法做到這一點?

回答

0

爲了記錄,這是我做了什麼來解決這個問題。我修改了supertest的代理對象和請求原型。 現在有一個叫做權威性導致Request.end調用AUTH結束前,第一種方法,然後恢復Request.end恢復到原來的狀態。

configure.js

var app = require('app'), 
    supertest = require('supertest'); 

// global 
agent = supertest.agent(app); 

(function(Request) { 
    'use strict'; 

    (function(_end) { 
    agent.auth = function() { 
     var authArgs = arguments; 
     Request.end = function() { 
     var endArgs = arguments; 
     var endResult = _end.apply(this.auth.apply(this, authArgs), endArgs); 
     Request.end = _end; 
     return endResult; 
     }; 
     return agent; 
    }; 
    })(Request.end); 
})(agent.post('').constructor.prototype); 

APP-test.js

describe("My App", function() { 

    describe('when logged out', function() { 
    it('should disallow anonymous user from doing things', function(done) { 
     agent.post('/do-things') 
     .send({thingName:'mythingname'}) 
     .expect(403) 
     .end(function(err, res) { 
     if (err) return done(err); 
     done(); 
     }); 
    }); 
    }); 

    describe('when invalid user', function() { 
    beforeEach(function(done) { 
     agent.auth('invalidusername', 'invaliduserpass'); 
     done(); 
    }); 

    it('should disallow unrecognized user from doing things', function(done) { 
     agent.post('/do-things') 
     .send({thingName:'mythingname'}) 
     .expect(403) 
     .end(function(err, res) { 
     if (err) return done(err); 
     done(); 
     }); 
    }); 
    }) 

    describe('when logged in', function() { 
    beforeEach(function(done) { 
     agent.auth('testusername', 'validuserpass'); 
     done(); 
    }); 

    it('should allow registered user to make a thing', function(done) { 
     agent.post('/make-a-thing') 
     .send({thingName:'mythingname'}) 
     .expect(201) 
     .end(function(err, res) { 
     if (err) return done(err); 
     res.body.should.have.property('thingUrl').and.to.match(/thing\/[0-9a-f]+$/); 
     done(); 
     }); 
    }); 

    it('should require name attribute to create a thing', function(done) { 
     agent.post('/make-a-thing') 
     .send({notaname:'notathingname'}) 
     .expect(409) 
     .expect('Content-Type', /json/) 
     .end(function(err, res) { 
     if (err) return done(err); 
     done(); 
     }); 
    }); 
    }); 

}); 

我的測試運行這樣的,所以配置腳本首先被執行:

mocha tests/configure.js tests/*-test.js