SORU
6 Aralık 2013, Cuma


Açısal uı router birim testi (leri Birleşik Devletleri)

Bazı sorunlar birim Açısal uı router üzerinde kurulu olan uygulama, yönlendirici test yaşıyorum. Test etmek istediğim devlet geçişler URL uygun (daha karmaşık testler daha sonra olacaktır, ama bu başlıyorum.) değişiklik olup olmadığı

İşte benim uygulama kodu: ilgili bölümü

angular.module('scrapbooks')
 .config( function($stateProvider){
    $stateProvider.state('splash', {
       url: "/splash/",
       templateUrl: "/app/splash/splash.tpl.html",
       controller: "SplashCtrl"
    })
 })

Ve test kodu:

it("should change to the splash state", function(){
  inject(function($state, $rootScope){
     $rootScope.$apply(function(){
       $state.go("splash");
     });
     expect($state.current.name).to.equal("splash");
  })
})

Stackoverflow benzer sorular (ve resmi uı router test kodu) devlet dolar sarma öneririz.git yeter uygulayın $Ara. Ama yapmadım ve devlet hala güncelleme yok. devlet. $mevcut.adı boş kalır.

CEVAP
12 Ocak 2014, Pazar


Nasıl da bu sorunu yaşıyor ve sonunda buldum.

İşte örnek bir devlet

angular.module('myApp', ['ui.router'])
.config(['$stateProvider', function($stateProvider) {
    $stateProvider.state('myState', {
        url: '/state/:id',
        templateUrl: 'template.html',
        controller: 'MyCtrl',
        resolve: {
            data: ['myService', function(service) {
                return service.findAll();
            }]
        }
    });
}]);

Birim test aşağıda URL w/ parametreler Test, ve kendi bağımlılıkları enjekte eden giderir yürütme şunlar olacak

describe('myApp/myState', function() {

  var $rootScope, $state, $injector, myServiceMock, state = 'myState';

  beforeEach(function() {

    module('myApp', function($provide) {
      $provide.value('myService', myServiceMock = {});
    });

    inject(function(_$rootScope_, _$state_, _$injector_, $templateCache) {
      $rootScope = _$rootScope_;
      $state = _$state_;
      $injector = _$injector_;

      // We need add the template entry into the templateCache if we ever
      // specify a templateUrl
      $templateCache.put('template.html', '');
    })
  });

  it('should respond to URL', function() {
    expect($state.href(state, { id: 1 })).toEqual('#/state/1');
  });

  it('should resolve data', function() {
    myServiceMock.findAll = jasmine.createSpy('findAll').andReturn('findAll');

    $state.go(state);
    $rootScope.$digest();
    expect($state.current.name).toBe(state);

    // Call invoke to inject dependencies and run function
    expect($injector.invoke($state.current.resolve.data)).toBe('findAll');
  });
});

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Air_Fooj

    Air_Fooj

    24 NİSAN 2009
  • macpulenta

    macpulenta

    9 EYLÜL 2006
  • TokShogun

    TokShogun

    6 HAZİRAN 2009