I'm currently in the process of setting up a test for an API call. In my attempt to create a fake server within the before method, I have encountered issues with testing the basic implementation using $.ajax
compared to my actual api call. Strangely, I am unable to see any requests being made in server.requests
. The ajax call triggers the error method with the message
cannot call method open of undefined
. I have already imported Sinon, sinonFakeHttps, and sinonFakeServer. Despite spending two days searching through forums, I haven't had any luck resolving this issue.
Below is the code snippet I've been working on:
describe('Warehouse Row', function (){
before(function(){
server = sinon.fakeServer.create();
server.autoRespond = true;
});
after(function(){
server.restore();
});
beforeEach(function(){
sut = new Sut();
sut.start();
});
it('should exist', function(){
should.exist(sut);
});
it('setting value to positive int should validate',function(done){
server.respondWith(200, { 'Content-Type': 'application/json' },'{ "stuff": "is", "awesome": "in here" }');
var callback = sinon.spy();
$.ajax({
url: '/something',
success: function(){
callback();
callback.should.have.been.called;
done();
},
error : function(err){
console.log(err);
}
});
});