Stormsson
Stormsson

Reputation: 1541

Angular testing with Jasmine

I'm trying to create some test with Jasmine to test a web app.

how do you test content insert from the page? I created an example included: If i test the controller alone,with mocked data, the sum function provided in the example does work, but testing with the page, it causes an error because the numbers are converted to string, so, what is the correct approach to this kind of tests?

Here is an example i created to understand the problem i'm facing:

angular.module('MyApp', [])
.controller('provaController', function ($scope) {

  $scope.firstNumber = 0;
  $scope.secondNumber= 0;
  $scope.result = 0;


  $scope.sum = function() {
    $scope.result =   $scope.firstNumber + $scope.secondNumber;
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp">
    <div ng-controller="provaController">
        <input type="text" ng-model="firstNumber" />
        <input type="text" ng-model="secondNumber" />

            <input type="button" ng-click="sum()" value="SUM"/>
    <br/>
        <input type="text" ng-model="result" />
        
        <p>First: -{{firstNumber}}- </p>
        <p>Sec: -{{secondNumber}}- </p>
        <p>Res: -{{result}}- </p>
    </div>
</div>

The pen example is here: http://codepen.io/anon/pen/wKYOxw

Upvotes: 1

Views: 48

Answers (1)

Peter Elliott
Peter Elliott

Reputation: 3322

the inputs are setting your model values to the string representation of the numbers, instead of the numbers themselves.

if you set the type attribute of your inputs to number instead of text you get the result that you expect.

I've updated your codepen with the change

Upvotes: 1

Related Questions