Пытаясь написать тест для наших запросов XmlhttpRequest Я пытаюсь проверить следующее
getMove(){
var image = 'placeholder'
let url = `http://localhost:8000/pong/bot?&bally=${Math.round(this.ball.position.y)}&paddley=${this.players[1].position.y}&reward=${this.aggregateReward}&img=${image}`
var that = this
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myArr = JSON.parse(this.responseText);
that._move = myArr['up'];
that.botUpdate(that._move);
that.responseReceived = true;
}
};
xmlhttp.open('GET', url, true);
xmlhttp.send();
}
Пытаясь смоделировать запрос, чтобы (это правильная идея?) Я написал этот тест для него
describe("getMove", function() {
it("should communicate data with XHR request", function() {
var xhr = {
open: jasmine.createSpy('open')
};
XMLHttpRequest = jasmine.createSpy('XMLHttpRequest');
XMLHttpRequest.and.callFake(function () {
return xhr;
});
submit();
expect(xhr.open).toHaveBeenCalled();
})
})
Тем не менее, я получаю представление не определено, я не совсем понимаю, что здесь происходит