ANGULARJS Unit Test (ii) detailed _ANGULARJS

Source: Internet
Author: User

Using object Simulation Injection

We can very easily use the Angularjs $provider service with an object to simulate a dependency and inject.

Examples are as follows

 Angular.module (' artists ', []).
  Factory (' Artists ', [' Imagestore ', function (Imagestore) {return
    {
      thumb:function ()] {return
        Imagestore.thumbnailurl (ID)}}
  ])

How to Achieve

How to determine the service

1. Create a reference to a URL that will be captured later by a mock, and a variable injected for artists

var URL;
VAR artists;

2, followed by the use of the $provide service in the Beforeeach method to register the simulated factory service. Use an object to simulate the Thumbnailurl method.

Beforeeach (Module (function ($provide) {
  $provide. Value (' Imagestore ', {
    thumbnailurl:function (id) {
      Url= '/thumb/' +id
    }}
)

3. Use the $injector service to inject this method, return this artists service and declare it with the variable you just created, which you can use later.

 Beforeeach (Inject (function ($inject) {
  artists= $inject. Get (' artists ');
 })

4, call artists to create a simple test

It (' Return the correct artist Thumbnailurl ', function () {
  artists.thumb (' 1 ');
  Expect (URL). Tobe ('/thumbs/1 ');
 })

5. Here is a complete example of using the $provide simulation test, which returns a definition of the Thumbnailurl method,

Describe (' factory:artists ', function () {
  var url;
  var artists;
  Beforeeach (module (' Artist '));
  Beforeeach (Module (function ($provide) {
    $provide. Value (' Imagestore ', {
      thumbnailurl:function (id) {
      url = '/thumbs/' + ID;}})  
    })
  ;
  Beforeeach (Inject function ($injector) {
    artists= $injector. Get (' artists ')
  })
  it (' return the Correct artist Thumbnailurl ', function () {
    artists.thumb (' 1 ');
    Expect (URL). Tobe ('/THUMB/1 ')
  })

 

Using spec to simulate a registered instance

To declare an instance of dependency injection, here is an example where there are two services, and the second service is injected into the first.

 Angular.module (' hiphop ', [])
  . Factory (' deejays ', [' $rootscope ', ' scratch ', function ($rootscope, scratch) {
    return{
      Originator: ' DJ Kool Herc ',
      technique:scratch.technique ()
    }
  }]
  . Factory (' Scratch ', [' $rootscope ', function ($rootscope) {
    return{
      technique:function () {return
        ' breakbeat ';
      }
  }}]

2,

Describe (' Service:deejays ', function () {
  Beforeeach (module (' hiphop '));
  var deejays;
  Beforeeach (Inject (function ($injector) {
    deejays= $injector. Get (' deejays ');
  }
  ) Beforeeach (Inject function ($provide) {
    $provide. Value (' Scratch ', jasmine.createspyobj (' scratch '), [' Technique ']
  )); It (' should return the correct originator ', function () {
    expect (deejays.originator). Tobe (' DJ Kool Herc ');
  })
 })

The above is the ANGULARJS unit test data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.