简体   繁体   English

如何检查方法是否已被称为茉莉花单元测试

[英]how to check if methods have been called jasmine unit test

I'm new to unit testing, and after reading the docs I'm still confused on how to do checks on asynchronous methods, as far as i understand I will need to use runs() and wait(). 我是单元测试的新手,在阅读了文档之后,我仍然对如何对异步方法进行检查感到困惑,据我所知,我将需要使用runs()和wait()。 However, instead of doing that, I could use spyOn to check if the method has been called instead. 但是,除了这样做,我可以使用spyOn来检查是否已调用该方法。

this is the code i want to test 这是我要测试的代码

  $scope.createMedicalServices = function(){
        if($scope.newTreatment.async_selected_treatment.id == null){
            $scope.newTreatment.description = $scope.newTreatment.description_no_id;
        }
        else{
            $scope.newTreatment.description = $scope.newTreatment.description;
        }

      $scope.newTreatment.no_cpt_code = $scope.newTreatment.no_cpt_code;
      $scope.newTreatment.cash_price = $scope.newTreatment.cash_price_input;
      $scope.newTreatment.average_price = $scope.newTreatment.average_price_input;
      $scope.newTreatment.service = $scope.newTreatment.async_selected_treatment.service;

      var returnPromiseObject;
      if ($scope.newTreatment.no_cpt_code) {
            returnPromiseObject = ProviderMedicalService.createNew($scope.newTreatment);
      }
      else if($scope.newTreatment.async_selected_treatment.id == null){
            returnPromiseObject = ProviderMedicalService.createNewCPT($scope.newTreatment);
      }
      else{
        returnPromiseObject = ProviderMedicalService.createExisting($scope.newTreatment);
      }

      returnPromiseObject.then(
        function (value){
          $scope.newTreatment.id = value.id;
          $scope.provider_medical_services.push($scope.newTreatment);
        },
        function (error){
          console.log(error);
        });
        };

  $scope.searchTreatments = function(value){
    return Treatment.find(value).then(function(values){
      return values;
    });
  };

and this is what i wrote so far, but its not much as i have no idea how to test it. 这是我到目前为止所写的内容,但由于我不知道如何进行测试而没有太多。

  it('search treament should have been called and return a value'),function(){
  scope.searchTreatments();

}

it('it should create medical service after submitting the form'),function(){
  scope.createMedicalServices();
  runs
}

Basically, I am content if the method just gets called properly, as the createMedicalService function is actually executed the moment the user clicks on the submit form, as its part of ng-submit on the html file. 基本上,我对方法是否被正确调用感到满意,因为createMedicalService函数实际上是在用户单击提交表单后立即执行的,作为html文件中ng-submit的一部分。

Help and suggestions greatly appreciated 帮助和建议,不胜感激

Unit testing can be a little confusing at first. 首先,单元测试可能会有些混乱。 There are a few principles that help with unit testing. 有一些原则可以帮助进行单元测试。

Singleness of Purpose. 目的的单一性。 A component should just do one thing. 一个组件应该只做一件事。 Consider breaking your code into smaller parts. 考虑将代码分成较小的部分。 Move logic out of controllers into factories or services. 将逻辑从控制器移到工厂或服务中。 These can be tested individually. 这些可以单独测试。

Inversion of Control. 控制反转。 Angularjs does a great job at providing IoC built in. Pass in your dependencies as stubs, fakes or mocks into your unit. Angularjs在提供内置的IoC方面做得很好。将您的依赖项作为存根,伪造品或模拟内容传递到您的单元中。

Only test the interface. 仅测试接口。 It is a bad practice to test the internal call structure of a unit. 测试单元的内部调用结构是一种不好的做法。 This ends up not testing the functionality, but rather the implimentation. 最终不测试功能,而是隐含的。 Instead think about this example. 而是考虑这个例子。

You have a controller that gets a rest service passed in called nameService. 您有一个控制器,该控制器获得传入的休息服务,称为nameService。 This controller has a method called submit(first, last) that when called, should called the method post(args) on the nameService. 该控制器具有一个称为Submit(first,last)的方法,该方法在调用时应在nameService上调用方法post(args)。

This would allow you to create a stub of the nameService, create a spy on the post() method and pass in the stub to the controller we are testing. 这将允许您创建nameService的存根,在post()方法上创建一个间谍,并将该存根传递给我们正在测试的控制器。 Then you can simply call submit() on the controller and test that it called the service. 然后,您可以简单地在控制器上调用Submit()并测试它是否调用了服务。

In essence, you test that if you input something into your unit, your unit will output it as expected. 本质上,您测试了是否将某些内容输入到您的单元中,您的单元将按预期输出它。

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

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