简体   繁体   English

单元测试Angularjs茉莉花

[英]Unit testing Angularjs jasmine

I'm trying to unit test a method in Angularjs controllers using jasminejs and Karma runner my method takes an image path in the argument and transforms that image to text (TESSERACT-OCR). 我试图使用jasminejs和KarmaRunner在Angularjs控制器中对方法进行单元测试,我的方法在参数中采用了图像路径并将该图像转换为文本(TESSERACT-OCR)。

when i try to call a unit test like this it does not work : 当我尝试调用这样的单元测试时,它不起作用:

TypeError: Attempted to assign to readonly property. TypeError:尝试分配给只读属性。 at 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");

})); }));

when i do the following: 当我执行以下操作时:

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");

})); }));

i get this error : 我收到此错误:

Expected undefined to match 'éàîè'. 预期未定义,以匹配“éàîè”。

can i access the $scope.textes.text value from the test ?? 我可以从测试中访问$ scope.textes.text值吗?

My question is how can i access the $scope.textes.text value that contains ocerised text from my test file ? 我的问题是我该如何访问包含来自测试文件中文本的$ scope.textes.text值? is it possible i don't think because it is inside an anonymous function.. Is this a correct unit test ? 我可能不认为是因为它位于匿名函数中。这是正确的单元测试吗? can i have more coverage in this unit test ?can anyone help me i'm new in testing with jasmine 我可以在这个单元测试中获得更多的覆盖范围吗?有人可以帮我吗,我是茉莉花测试的新手。

Normally when unit testing an HTTP call, you set up the expectation against $httpBackend , call the function you are testing normally, then call $httpBackend.flush() to fake the expected HTTP response and complete the call synchronously, then test the outcome. 通常,在对HTTP调用进行单元测试时,请针对$httpBackend设置期望值,调用通常测试的函数,然后调用$httpBackend.flush()伪造期望的HTTP响应并同步完成调用,然后测试结果。

So, taking a stab at your test, it'd probably look more like this.... 因此,对您的测试进行一次测试,可能看起来更像这样。

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')
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM