我想測試ng-if
中沒有ng的輸入是否存在 - 如果測試完全通過但不與ng-if
通過。Angular 1/ui-select/Jasmine - 測試元素是否存在於ng中 - 如果
在我的模板,我有:
<div ng-if="$ctrl.doShowAir">
<input class="form-control" type="text">
</div>
的doShowAir
是一個功能doShow()
內的變量在以前的UI選選擇的選擇,當它應該是true
<ui-select ng-model="$ctrl.parking.parkingType"
on-select="$ctrl.doShow()">
<ui-select-match>
<span>{{ $select.selected.label }}</span>
</ui-select-match>
<ui-select-choices repeat="item in $ctrl.projectReferences.parkingType | filter: { label: $select.search }">
<span ng-bind-html="item.label"></span>
</ui-select-choices>
</ui-select>
功能:
doShow() {
this.doShowAir = (this.parkingType.labelKey === 'parking_type.air')
}
而uni t檢驗:
import angular from 'angular'
import 'angular-mocks'
let scope
let rootScope
let compile
let htmlElement
let ctrl
fdescribe('projectExteriorParking',() => {
beforeEach(() => {
angular.mock.module('ProjectExteriorParkingModule')
angular.mock.module('ui.select')
})
beforeEach(inject((_$compile_, _$rootScope_) => {
rootScope = _$rootScope_
compile = _$compile_
scope = rootScope.$new()
scope.parking = {}
htmlElement = compile(`<project-exterior-parking parking="project.realEstateProjectProduct.parkings"></project-exterior-parking>`)(scope)
rootScope.$digest()
}))
beforeEach(inject(($componentController) => {
let bindings = {
parking: {},
projectReferences: {}
}
ctrl = $componentController('projectExteriorParkingModule', null, bindings)
}))
it('should contain two input',() => {
const inputItems = htmlElement.get(0).querySelectorAll('input')
expect(inputItems.length).toBe(2)
})
})
我如何可以模擬該變量shouldShowAir
是真實的,還是模擬的用戶界面,選擇因爲我沒有訪問doShow()
函數調用函數的選擇on-select="$ctrl.doShow()"
。 或者你如何「手動」除了輸入添加到測試和編譯它<project-exterior-parking parking="project.realEstateProjectProduct.parkings"></project-exterior-parking>
1.什麼是你想用做方法的值:'CTRL = $ componentController(「projectExteriorParkingModule」,空,綁定)'在第二個' beforeEach'塊?這是試圖讓上面的編譯組件的控制器?你可以用'htmlElement.controller('projectExteriorParking')'獲得元素控制器 – kidroca