我正在尝试使用通过Teaspoon运行的Jasmine在Angular中创建一些单元测试.测试正在运行,但是我有一个简单的测试只是为了测试是否存在失败的控制器.我有以下测试设置.
//= require spec_helper
require("angular");
require("angular-mocks");
var app = require("./app");
describe("My App", function() {
describe("App Controllers", function() {
beforeEach(module("app"))
it("Should have created an application controller", inject(function($rootScope, $controller){
var scope = $rootScope.$new();
ctrl = $controller("ApplicationCtrl", { $scope: scope });
}));
})
})
Run Code Online (Sandbox Code Playgroud)
需要的语句由Browserify处理,它处理我的依赖项,但我也可以挂钩我用于规范助手的链接.
在我需要的应用程序内部
require("angular");
var controllers = require("./controllers");
var app = angular.module("app", [
"app.controllers"
]);
exports.app = app;
Run Code Online (Sandbox Code Playgroud)
当我运行此测试时,我得到以下错误
Failure/Error: TypeError: '[object Object]' is not a function (evaluating 'module("aialerts")')
Run Code Online (Sandbox Code Playgroud)
我花了很长时间试图解决这个问题,但我不知道发生了什么.任何帮助赞赏.
$httpBackend.when('')和之间有什么区别$httpBackend.expect('')?
我不知道这两种方法的区别.另外angularjs api doc对我没有帮助.
API文档链接:https://docs.angularjs.org/api/ngMock/service/ $ httpBackend
我正在尝试以角度设置e2e测试套件,并且需要使用$ httpBackend返回预设响应.如果我能够返回文件内容,那将是很好的,例如
$httpBackend.whenPOST('/phones').respond(function(method, url, data) {
return getContentOf("/somefile");
});
Run Code Online (Sandbox Code Playgroud)
我尝试使用$ http,这是一些东西
$httpBackend.whenPOST('/phones').respond(function(method, url, data) {
return $http.get("/responses/phones.js");
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用,猜角度不支持从$ httpBackend返回承诺?
我可以做的一种方法是引用带有应用程序加载响应的js文件,并将文件的内容分配给变量,但是能够按需加载数据会更好.
有没有办法返回一个模仿调用的HttpPromise(或类似的东西)$http?我想设置一个全局变量,指示是否发出了真正的HTTP请求,或者是否使用伪数据返回了假的HttpPromise对象.
例如,我有一个与此类似的服务:
angular
.module('myservice')
.factory('MyService', ['$http', function($http) {
return {
get : function(itemId) {
if (isInTestingMode) {
// return a promise obj that returns success and fake data
}
return $http.get("/myapp/items/" + itemId);
}
};
} ]);
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我调用上述类似于此的服务:
// Somewhere in my controller
MyService.get($scope.itemId)
.success(function(data) {
$scope.item = data;
})
.error(function(data, status, headers, config) {
$scope.notFound = true;
});
Run Code Online (Sandbox Code Playgroud)
我试图不改变控制器代码; 在我的"isInTestMode"中,我希望success和error链接仍然有效.是否有可能以HttpPromise我在服务中描述的方式伪造一个?
下面是上面的"MyService"修订版(一个片段),其中包含一个success和error在promise对象上.但是,我该如何执行该success方法?
return { …Run Code Online (Sandbox Code Playgroud) 我使用Angular.js $httpBackend来测试一些包装$http调用的服务(这是在ngMock中,而不是 ngMockE2E).
看来,喜欢的东西expect和when对的URL查询参数的顺序是敏感的.例如,如果我这样做,$httpBackend.when('POST','/apiCall?X=1&Y=2').respond(/* ... */)或者如果我在$httpBackend.expectPOST('/apiCall?X=1&Y=2')URL中有Y = 2和X = 1而不是X = 1和Y = 2,则会出现URL不匹配.
我想以这样一种方式编写我的测试,即被测试的服务可以自由地更改URL查询字符串参数的顺序而不会破坏测试.我在$ httpBackend文档中找不到任何解决方法.这样做的正确方法是什么?
angularjs angular-http httpbackend angularjs-http angular-mock
我发现角度模拟阻止所有请求BY DEFAULT,并迫使我"直接"发生我想要的东西,这令人非常沮丧.
有时我只想用模拟测试1个url,我必须为每个"意外请求"错误跳过严重的箍.
我不知道正则表达式,我不喜欢正则表达式,我不想使用正则表达式!
看一下我需要的一个简单的模拟这个可怕的代码
$httpBackend.whenGET(/\/atlas\/account\/[0-9]+$/)
.respond(atlasAccounts[0]);
$httpBackend.whenGET(/\/scripts$/).passThrough();
$httpBackend.whenGET(/^\w+.*/).passThrough();
$httpBackend.whenPOST(/^\w+.*/).passThrough();
Run Code Online (Sandbox Code Playgroud)
为什么这不能简化为一行???
$httpBackend.whenGET(/\/atlas\/account\/[0-9]+$/)
.respond(atlasAccounts[0]);
Run Code Online (Sandbox Code Playgroud)
或者甚至更好,为什么它不支持该死的通配符?他们是否试图让开发人员的生活变得更难?
$httpBackend.whenGET("/atlas/account*")
.respond(atlasAccounts[0]);
Run Code Online (Sandbox Code Playgroud)
这就是我所需要的,只要它是直观的......
有没有办法在ngMock中禁用这个全有或全无的约定,并且只是拦截我明显嘲笑的网址?
我想Jasmine测试Welcome.go已被调用.欢迎是一个有角度的服务.
angular.module('welcome',[])
.run(function(Welcome) {
Welcome.go();
});
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的测试:
describe('module: welcome', function () {
beforeEach(module('welcome'));
var Welcome;
beforeEach(inject(function(_Welcome_) {
Welcome = _Welcome_;
spyOn(Welcome, 'go');
}));
it('should call Welcome.go', function() {
expect(Welcome.go).toHaveBeenCalled();
});
});
Run Code Online (Sandbox Code Playgroud)
注意:
我想为我的应用程序测试一个Angular控制器fooApp,定义如下:
var fooApp = angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]);
...
Run Code Online (Sandbox Code Playgroud)
控制器,MainCtrl定义为:
"use strict";
fooApp.controller('MainCtrl', function ($scope, $rootScope, fooService) {
...
}
Run Code Online (Sandbox Code Playgroud)
所以我已经测试了几种创建测试的方法,比如这个:
'use strict';
describe('MainController test', function () {
var scope;
var controller;
beforeEach(function () {
angular.mock.module('ngRoute', []);
angular.mock.module('ngAnimate', []);
angular.mock.module('hmTouchEvents', []);
angular.module('cwfApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]);
angular.mock.inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('MainCtrl', {
$scope: scope
});
});
});
it('should display a list', function () {
console.log('-------------- Run Test 1 …Run Code Online (Sandbox Code Playgroud) 我在这个问题上搜索了很多,但找不到解决方案.
我正在试图模拟我的后端,经过充分测试,所以我可以完全隔离我的前端.我尝试过使用protractor-http-mock以及角度模拟的各种努力.
使用HttpBackend解决了角度模拟方法后,我在启动量角器测试时遇到了这个错误:
MBP:test-site admin$ protractor protractor.conf.js
Using ChromeDriver directly...
[launcher] Running 1 instances of WebDriver
[launcher] Error: ReferenceError: window is not defined
at Object.<anonymous> (/Users/Ed/Sites/F4F/web/node_modules/angular/angular.js:30426:4)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/Users/Ed/Sites/F4F/web/node_modules/angular/index.js:1:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
[launcher] Process exited with error code 100
Run Code Online (Sandbox Code Playgroud)
这是我的protractor.conf.js
exports.config = {
directConnect: true,
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
}, …Run Code Online (Sandbox Code Playgroud) 我正在使用具有角度的MeteorJS并且想要测试控制器.我的控制器使用$ reactive(this).attach($ scope).如果调用此方法,我需要检查.
我为间谍创造了类似的东西:
var $reactive = function(ctrl) {
return {
attach:function(scope) {}
}
};
Run Code Online (Sandbox Code Playgroud)
所以我可以这样称呼它:
$reactive('aaa').attach('bbb');
Run Code Online (Sandbox Code Playgroud)
我怎么能在测试中做到这一点?
spyOn($reactive, 'attach');
Run Code Online (Sandbox Code Playgroud)
不行.我得到错误:attach()方法不存在
以及如何检查它是否被调用?这是好的电话?
expect($reactive).toHaveBeenCalledWith(controller);
Run Code Online (Sandbox Code Playgroud)
如何用args(范围)调用函数attach?
angular-mock ×10
angularjs ×9
jasmine ×3
javascript ×3
httpbackend ×2
unit-testing ×2
angular-http ×1
karma-runner ×1
meteor ×1
protractor ×1
teaspoon ×1