使用Angular.js,Mocha和Jasmine进行测试我对DeviceVal
值文件进行了测试:
describe("load balancer device: value", function() {
var DeviceVal;
DeviceVal = null;
beforeEach(function() {
return module("main.loadbalancer");
});
beforeEach(inject(function(_DeviceVal_) {
return DeviceVal = _DeviceVal_;
}));
return it("should default the id to 'device not loaded'", function() {
return expect(DeviceVal).toEqual({
readonly: false,
lb: "device not loaded"
});
});
});
对于我的其他测试之一,我使用了DeviceVal
值文件:
describe("load balancer controller: readonly", function() {
$scope = DeviceVal = LoadBalancerSvc = SearchSvc = $state = $sce = null;
beforeEach(function() {
module('main');
});
beforeEach(inject(function($controller, $rootScope, _$state_, _LoadBalancerSvc_, _DeviceVal_) {
$scope = $rootScope.$new();
$state = _$state_;
LoadBalancerSvc = _LoadBalancerSvc_;
DeviceVal = _DeviceVal_;
$controller('HeaderCtrl', {$scope: $scope});
//to resolve all the promises we have on the mocked service
$scope.$digest();
lb = { ha_status: "secondary", id: "34584"};
this.lb = lb;
spyOn(LoadBalancerSvc, "searchDevice").and.returnValue(
{get: function() {
return { then: function(fn) { fn(lb)} }}});
spyOn($state, "go");
}));
it('scope data memembers to have their stuff', function() {
$scope.searchButton.submit();
expect(lb).toEqual(this.lb);
expect(lb['ha_status']).toEqual("secondary");
expect(DeviceVal.readonly).toBe(true);
expect($state.go).toHaveBeenCalledWith("main.loadbalancer.vips", {id: this.lb.id});
});
});
我遇到的问题是我想手动将DeviceVal
的第二次测试中的值设置为自定义值。但相反,测试期望值为第一个DeviceVal
测试文件中的值。它抱怨说:
Expected { readonly : true, lb : { ha_status : 'secondary', id : '34584' } } to equal { readonly : false, lb : 'device not loaded' }.
如何进行第二次测试,我可以手动设置值DeviceVal.lb = { ha_status: "secondary", id: "34584"};
并拥有expect(lb['ha_status']).toEqual("secondary");
和expect(DeviceVal.readonly).toBe(true);
?
更新:
这是模拟DeviceVal,但我仍然得到Error: Expected { readonly : true, lb : { ha_status : 'secondary', id : '34584' } } to equal { readonly : fals
e, lb : 'device not loaded' }.
:
describe("load balancer controller: readonly", function() {
$scope = DeviceVal = LoadBalancerSvc = SearchSvc = $state = $sce = null;
beforeEach(function() {
module('main');
});
beforeEach(inject(function($controller, $rootScope, _$state_, _LoadBalancerSvc_, _DeviceVal_) {
$scope = $rootScope.$new();
$state = _$state_;
LoadBalancerSvc = _LoadBalancerSvc_;
DeviceVal = _DeviceVal_;
$controller('HeaderCtrl', {$scope: $scope});
//to resolve all the promises we have on the mocked service
$scope.$digest();
//MY ATTEMPT AT MOCKING THIS DATA
DeviceVal.lb = { ha_status: "secondary", id: "34584"};
DeviceVal.readonly = true;
spyOn(LoadBalancerSvc, "searchDevice").and.returnValue(
{get: function() {
return { then: function(fn) { fn(DeviceVal.lb)} }}});
spyOn($state, "go");
}));
it('scope data memembers to have their stuff', function() {
$scope.searchButton.submit();
expect(DeviceVal.lb).toEqual({ha_status: "secondary", id: "34584"});
expect(DeviceVal.lb['ha_status']).toEqual("secondary");
expect(DeviceVal.readonly).toBe(true);
expect($state.go).toHaveBeenCalledWith("main.loadbalancer.vips", {id: DeviceVal.lb.id});
});
});
答案 0 :(得分:0)
修正了它。从http://angular-tips.com/blog/2014/06/introduction-to-unit-test-controllers/开始,我创建了这样的模拟数据:
describe("load balancer controller: readonly", function() {
$scope = DeviceVal = LoadBalancerSvc = SearchSvc = $state = $sce = null;
beforeEach(function() {
eviceVal = {}
module('main', function($provide) {
$provide.value('DeviceVal', eviceVal);
});
inject(function() {
eviceVal.lb = { ha_status: "secondary", id: "34584"};
eviceVal.readonly = true;
});
});