Unit testing Angularjs jasmine

badaboum

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).

when i try to call a unit test like this it does not work :

TypeError: Attempted to assign to readonly property. 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 ??

My question is how can i access the $scope.textes.text value that contains ocerised text from my test file ? 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

moribvndvs

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.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

expect() in .then() - jasmine unit testing, AngularJS

From Dev

Unit-Testing a filter with Jasmine in AngularJS

From Dev

mock angularjs service for unit testing using jasmine

From Dev

Unit-testing slider directive with AngularJS + Jasmine

From Dev

Unit-Testing a filter with Jasmine in AngularJS

From Dev

Unit testing of Services / Factories - AngularJS - Jasmine

From Dev

ReferenceError : AngularJS is not defined on Karma with Jasmine unit testing

From Dev

AngularJS Unit Testing Controller w/ service dependency in Jasmine

From Dev

AngularJS Jasmine Unit Testing - Controller method is never called

From Dev

AngularJS Jasmine Unit Testing - Controller method is never called

From Dev

When unit-testing my angularJS application with Jasmine, $interval is undefined

From Dev

Unit testing $modal with Jasmine

From Dev

Testing AngularJS resource with Jasmine

From Dev

Angularjs and jasmine, testing promise

From Dev

AngularJS and Jasmine testing factory

From Dev

Unit testing with Jasmine, mocking a constructor

From Dev

Jasmine unit testing an element is not :hidden

From Dev

jasmine unit testing was method called

From Dev

Jasmine unit testing an element is not :hidden

From Dev

Jasmine, Unit testing XMLHttpRequest with Promises

From Dev

Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

From Dev

Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

From Dev

Unit Testing AngularJS Service

From Dev

Unit testing promises in AngularJS

From Dev

AngularJS unit testing with ReSharper

From Dev

Unit testing controllers in angularJS

From Dev

Cordova & AngularJS unit testing

From Dev

AngularJS Unit testing $location

From Dev

Unit testing a service in angularJS

Related Related

  1. 1

    expect() in .then() - jasmine unit testing, AngularJS

  2. 2

    Unit-Testing a filter with Jasmine in AngularJS

  3. 3

    mock angularjs service for unit testing using jasmine

  4. 4

    Unit-testing slider directive with AngularJS + Jasmine

  5. 5

    Unit-Testing a filter with Jasmine in AngularJS

  6. 6

    Unit testing of Services / Factories - AngularJS - Jasmine

  7. 7

    ReferenceError : AngularJS is not defined on Karma with Jasmine unit testing

  8. 8

    AngularJS Unit Testing Controller w/ service dependency in Jasmine

  9. 9

    AngularJS Jasmine Unit Testing - Controller method is never called

  10. 10

    AngularJS Jasmine Unit Testing - Controller method is never called

  11. 11

    When unit-testing my angularJS application with Jasmine, $interval is undefined

  12. 12

    Unit testing $modal with Jasmine

  13. 13

    Testing AngularJS resource with Jasmine

  14. 14

    Angularjs and jasmine, testing promise

  15. 15

    AngularJS and Jasmine testing factory

  16. 16

    Unit testing with Jasmine, mocking a constructor

  17. 17

    Jasmine unit testing an element is not :hidden

  18. 18

    jasmine unit testing was method called

  19. 19

    Jasmine unit testing an element is not :hidden

  20. 20

    Jasmine, Unit testing XMLHttpRequest with Promises

  21. 21

    Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

  22. 22

    Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

  23. 23

    Unit Testing AngularJS Service

  24. 24

    Unit testing promises in AngularJS

  25. 25

    AngularJS unit testing with ReSharper

  26. 26

    Unit testing controllers in angularJS

  27. 27

    Cordova & AngularJS unit testing

  28. 28

    AngularJS Unit testing $location

  29. 29

    Unit testing a service in angularJS

HotTag

Archive