運行一個單元測試失敗,並執行以下操作:設置角單元測試多個指令[...],要求新的/分離的範圍上
Error: [$compile:multidir] Multiple directives [sendEmail, sendEmail] asking for new/isolated scope on: <send-email resolve="">
我的單元測試如下。它試圖在不同的測試中兩次編譯組件。它的塊是相同的。但第二次添加失敗。
import angular from 'angular';
import 'angular-mocks';
import {sendEmail} from './send-email.js';
describe('component - sendEmail',() => {
let $rootScope;
let $compile;
beforeEach(() => {
angular
.module('app')
.component('sendEmail', sendEmail);
angular.mock.module('app');
inject(function(_$rootScope_, _$compile_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
});
});
it('...',() => {
const element = $compile('<send-email resolve=""></send-email>')($rootScope);
$rootScope.$digest();
expect(element.find('.hp-send-email-container').length).toEqual(1);
});
// adding it will fail the test
it('...',() => {
const element = $compile('<send-email resolve=""></send-email>')($rootScope);
$rootScope.$digest();
expect(element.find('.hp-send-email-container').length).toEqual(1);
});
});
到目前爲止,我已嘗試重置範圍並銷燬組件以及這些組合。但它沒有任何影響。
it('...',() => {
// tried creating and using a new scope for $compile
// const $scope = $rootScope.$new(true);
const element = $compile('<send-email resolve=""></send-email>')($rootScope);
$rootScope.$digest();
expect(element.find('.hp-send-email-container').length).toEqual(1);
// tried removing the element as well as destroying the scope
// element.remove();
// $scope.$destroy();
});
最後我想實現的是用不同的輸入多次編譯組件並查看輸出。也許我正在接近完全錯誤的問題。歡迎任何建議。