我正在尝试使用jasminejs和Karma runner在Angularjs控制器中对方法进行单元测试 我的方法在参数中采用图像路径并将该图像转换为文本(TESSERACT-OCR)。
当我尝试调用这样的单元测试时,它不起作用:
TypeError:尝试分配给readonly属性。 在workFn
it('has to return text from image', inject(function($httpBackend) {
$scope.ocr("./image.png");
$httpBackend.expectPOST('/ocr').respond();
expect( scope.oceriser.bind("./ocr.png")).toMatch("ocr");
}));
当我执行以下操作时:
it('has to return text from image', inject(function($httpBackend) {
$scope.ocr("./image.png");
$httpBackend.expectPOST('/ocr').respond();
expect($scope.ocr("./ocr.png")).toMatch("ocr");
}));
我收到此错误:
预期未定义以匹配'éàîè'。
我可以从测试中访问$ scope.textes.text值吗?
我的问题是如何从我的测试文件中访问包含ocerised文本的$ scope.textes.text值?是不是可能我不认为因为它是在一个匿名函数内... 这是一个正确的单元测试吗?我可以在这个单元测试中获得更多的报道吗?任何人都可以帮助我,我是茉莉花测试新手
答案 0 :(得分:1)
通常在单元测试HTTP调用时,设置对$httpBackend
的期望,调用正常测试的函数,然后调用$httpBackend.flush()
伪造预期的HTTP响应并同步完成调用,然后测试结果。
所以,对你的测试进行一次尝试,它看起来可能更像......
it('has to return text from image', inject(function($httpBackend) {
var expected = {}; // the value expected from the HTTP request
// set up the http expectation. this tells angular what you expect to have called through $http when flush() is called
$httpBackend.expectPOST('/oceriser').respond(expected);
// call the function you are testing
scope.oceriser('./image.png');
// flushes the pending fake HTTP response causing the call to oceriser above to complete synchronously, and the function will continue normally
$httpBackend.flush();
// now, verify that oceriser() did what you expect when the http call succeeds. you'll need to figure this out, but in this example, i assume textes contains no elements initially, but after oceriser is called, it will contain one element with the text property equal to expected
expect(scope.textes.length).toBe(1);
expect(scope.textes[0].text).toBe(expected);
expect(scope.textes[0].source).toBe('./image.png')
}));