【问题标题】:i want to write unit test case for my angular js app我想为我的 Angular js 应用程序编写单元测试用例
【发布时间】:2020-12-08 16:17:32
【问题描述】:

我有一个天气应用,它使用https://metaweather.com/api/ 来获取 js 代码的天气

angular.module('ourAppApp')
.controller('MainCtrl', function($scope,apiFac) {

    $scope.displayCity = false;
    $scope.displayWeather = false;
    $scope.today = new Date();
    $scope.searchCity = function(){

        if($scope.cityInput != ""){ 
          apiFac.searchCity($scope.cityInput).then(function(response){
            $scope.searchResponse = response;
            $scope.displayCity = true;
          })
        }   
    }

    $scope.getcitydetails = function(item){
      $scope.cityInput = "";
      $scope.displayCity = false;
      $scope.selectedCity = item;

      apiFac.weatherReport(item.woeid).then(function(response){
        $scope.weatherResponse = response;
        $scope.displayCity = false;
        $scope.displayWeather = true;
      })
    }


})

index.html 是这样的

    <!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-route.min.js"></script>
  <base href="/">
  <link rel="stylesheet" href="styles/style.css">
  <title>Weather app</title>
</head>
<body ng-app="ourAppApp">
  <div ng-view="">
  </div>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
  <script src="scripts/app.js"></script>
  <script src="scripts/controllers/main.js"></script>
  <script src="scripts/factory/apiFac.js"></script>
</body>
</html>

我想为 angularjs 函数编写单元测试用例,我是单元测试新手,任何帮助都将不胜感激,提前感谢

【问题讨论】:

标签: javascript html angularjs unit-testing


【解决方案1】:

我建议你可以使用 cucumberjs。它使用简单的小黄瓜语法和用户友好。 https://github.com/cucumber/cucumber-js 如果您想到的问题是关于 API 调用...那么请记住,当涉及到 javascript 时,有两件事需要测试。

  1. 测试打字稿视图模型 - 这可以由 cucumberjs 完成
  2. 测试用户界面 - 这可以通过支持 chromedriver 的 Selenenium 来完成。如果您在后端使用 C#,则使用 specflow 进行单元测试。或者甚至可以直接使用硒。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多