Chcę przetestować niektóre funkcje w module node.js. Myślę, że kpiny z trzeciego modułu byłyby pomocne. W szczególności w celu uniknięcia uderzenia się baza danychPrześmiewanie modułów w Node.js do testowania jednostkowego
# models/account.coffee
register = (email, password)->
sha_sum.update(password)
pw = sha_sum.digest('hex')
user =
email: email
password: sha_sum.digest('hex')
users_db.save user, (err, doc)->
register_callback(err)
account_module =
register: register
module.exports = account_module
Jest to moduł, który chcę przetestować
# routes/auth.coffee
account = require '../models/account'
exports.auth =
post_signup: (req, res)->
email = req.body.email
password = req.body.password
if email and password
account.register(email, password)
res.send 200
else
res.send 400
Chcę być w stanie sprawdzić, czy uderzanie ten link z poprawnym ciała w post nazywa Funkcja account.register
, ale nie chcę, aby test trafił do bazy danych. Być może jeszcze nie zaimplementowałem modułu konta.
Jasmine Spec # specyfikacje/auth.test.coffee opisać 'zarejestruj' ->
request = require 'request'
it 'should signup a user with username and password', (done)->
spyOn(account, 'register') # this does not work, account.register still called
url = root + '/signup'
headers =
"Content-Type": "application/json"
data =
email: '[email protected]'
password: 'pw'
body = JSON.stringify(data)
request {url: url, method: 'POST',json: data, headers: headers }, (err, response, body)->
expect(response.statusCode).toEqual(200)
done()
Mam spojrzał na kilka bibliotek drwiących dla node.js (https://github.com/easternbloc/Syringe, https://github.com/felixge/node-sandboxed-module), ale do tej pory bez powodzenia. Cokolwiek wypróbuję w specyfikacji, account.register
zawsze zostanie wykonane. Czy to całe podejście jest wadliwe?
Wygląda horaa (https://github.com/arunoda/horaa) może działać –
Spróbuj sinon.js, doskonały skrótowej/spy/mock/mockServer library. – nottinhill