2014-09-12 6 views
0

Я использую Yeoman для создания моего проекта AngularJS. Попытка проверить директиву с помощью Jasmine. Эта директива оценивает элемент ul. Я проверяю, успешно ли это. Как написать синтаксис с помощью Jasmine? Я следую примеру, который генерирует Yo angular:directive. Я также хотел бы знать, приближаюсь ли я к этому правильно? Любые указатели, чтобы правильно изучить Жасмин?Проверьте, соответствует ли директива определенному элементу HTML?

В дополнение к ответу ниже, мне удалось установить и использовать jasmine-jquery для выполнения моей работы.

ответ

0

Чтобы проверить директиву, вы можете составить свой шаблон HTML в тестах жасмин:

describe('Unit testing great quotes', function() { 
var $compile; 
var $rootScope; 

// Load the myApp module, which contains the directive 
beforeEach(module('myApp')); 

// Store references to $rootScope and $compile 
// so they are available to all tests in this describe block 
beforeEach(inject(function(_$compile_, _$rootScope_){ 
    // The injector unwraps the underscores (_) from around the parameter names when matching 
    $compile = _$compile_; 
    $rootScope = _$rootScope_; 
})); 

it('Replaces the element with the appropriate content', function() { 
    // Compile a piece of HTML containing the directive 
    var element = $compile("<your-directive></your-directive>")($rootScope); 

    // fire all the watches, so the scope expression {{1 + 1}} will be evaluated 
    $rootScope.$digest(); 

    // Check that the compiled element contains the templated content 
    expect(element.find("ul").length).toBeGreaterThan(1); 
}); 
}); 

https://docs.angularjs.org/guide/unit-testing

В учебнике на домашней странице Жасмин на самом деле очень хороший http://jasmine.github.io/2.0/introduction.html

+0

Большое спасибо ! Мне удалось использовать синтаксис jQuery, используя jasmine-jquery. Но вы объяснили здесь несколько деталей, что очень полезно. –

Смежные вопросы