Fla*_*ape 7 javascript unit-testing jasmine angularjs httpbackend
我已经尝试过并试图让它发挥作用.该文件是简洁的,充其量:
resetExpectations(); - 重置所有请求期望,但保留所有后端定义.通常,如果要重用$ httpBackend mock的相同实例,则可以在多阶段测试期间调用resetExpectations.
每次调用第二个请求时,我的结果总是有第一个结果的数据.看看这个小提琴http://jsfiddle.net/tbwn1gt0/2/我在第一次刷新后重置期望,然后设置新的期望/结果然后再次刷新以产生不正确的数据.
// --- SPECS -------------------------
var url = '/path/to/resource';
var result = '';
describe('$httpBackend', function () {
it("expects GET different results in subsequent requests", inject(function ($http, $httpBackend) {
successCallback = function(data){
result = data;
}
// Create expectation
$httpBackend.expectGET(url).respond(200, 'mock data');
// Call http service
$http.get(url).success(successCallback);
// flush response
$httpBackend.flush();
console.log( result ); // logs 'mock data'
// Verify expectations
expect( result ).toContain('mock data'); // works as it should
// reset the expectations
$httpBackend.resetExpectations();
// set the fake data AGAIN
$httpBackend.expectGET(url).respond(200, 'doof the magic cragwagon');
// get the service AGAIN
$http.get(url).success(successCallback);
expect( result ).toContain('doof'); // does not work, result is original result
console.log( result ); // logs 'mock data'
}));
});
// --- Runner -------------------------
(function () {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function (spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function () {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
Run Code Online (Sandbox Code Playgroud)
我尝试过的其他事情包括在resetExpectations中添加一个afterEach(将每个请求放在一个新的it语句中).以及一系列其他随机尝试.如果它试图将预期的URL更改为不期望的内容,则会出现错误 - 因此我知道请求至少通过httpBackend处理.
这是一个缺陷还是我错误地实现了它?
它.resetExpectations()
确实按您的预期工作,但您只是忘记刷新第二个请求的 http 请求。
// set the fake data AGAIN
$httpBackend.expectGET(url).respond(200, 'doof the magic cragwagon');
// get the service AGAIN
$http.get(url).success(successCallback);
$httpBackend.flush(); // flush the second http request here
expect( result ).toContain('doof'); // does not work, result is original result
console.log( result ); // logs 'mock data'
Run Code Online (Sandbox Code Playgroud)
JSFiddle 示例: http: //jsfiddle.net/4aw0twjf/
附言。实际上,这$httpBackend.resetExpectations()
对于您的测试用例来说并不是必需的。
归档时间: |
|
查看次数: |
4608 次 |
最近记录: |