我正在使用coffeescript编写节点应用程序并使用mocha.js进行一些TDD。请有人指出我正确的方式来编写测试。
我想做什么: “ra.do_get”的假响应然后调用方法'all_tests'来分析返回值
var RequestAdapter = require('./request_adapter');
class QueryHandler
constructor: (@adapter) ->
@klass = "QueryHandler"
@url = "http://localhost:#{port[@adapter]}/"
logger.debug "[#{@klass}] creating .."
//RequestAdapter is another class doing some GET/POST etc (using request NPM)
@ra = new RequestAdapter(@url)
all_tests: (cb) ->
logger.info "#{@adapter}: requesting all test - #{adapter_apis.ALL_TESTS_SESSION}"
_url = @url + adapter_apis.ALL_TESTS_SESSION
@ra.do_get _url, (error , response) ->
if error
return cb({error: true, message: "#{@adapter} adapter errored out"})
else
return cb(null,response)
return
由于
答案 0 :(得分:0)
最后它奏效了。最初obj我试图窥探是错误的
describe('#all_tests()', function(){
it('should return all tests', function(done){
var qh_obj = new qh("FAKE_ADAPTER");
var spy = sinon.spy(qh_obj.ra,'do_get');
qh_obj.all_tests(function(e,r) {});
(spy.callCount).should.equal(1);
done();
});
}); // describe