我正在使用Jasmine来测试我的Angular应用程序.如果使用类似的方法调用函数的方法,则很容易测试:
spyOn($rootScope, "$emit");
expect($rootScope.$emit).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)
但我无法找到一种方法来检查函数何时被调用(没有方法),例如我正在使用$anchorScroll(); 在一个控制器,我不知道在哪里将上述代码应用于这个人.我已经看到了他们使用的一些Jasmine示例expect(window.myFunction()).toHaveBeenCalled(),但这不适用于Angular的DI.
我正在写量角器测试用例.我想点击编辑按钮.当我检查id它时,找不到它.
码:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Code</th>
<th>Start Date</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="batch in batches.list">
<td><a ui-sref="root.courses.detail.batches.assessments({ batch_id: batch.id,assessment_status: 'published'})">BID00{{batch.id}}</a></td>
<td>{{batch.start_date}}</td>
<td>
<button id="edit" type="button" class="btn btn-default btn-sm" tooltip-placement="top" tooltip="Edit" ng-controller="batchesCtrl" ng-click="edit_batch(batch.id)"><i class="glyphicon glyphicon-edit"></i></button>
<button type="button" id="delete(batch.id)" class="btn btn-danger btn-sm" tooltip-placement="top" tooltip="Delete" ng-click="remove_batch(1, batch.id)" confirmation-needed="Do you really want to delete this batch?"><i class="glyphicon glyphicon-trash" ></i></button>
</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我怎样才能使它工作?
开始使用HotTowelAngular模板,我正在设置单元测试.陷入困境.
我正在尝试在我的控制器中测试一个正好调用另一个名为"log"的函数的函数.这个"log"是一个存储在私有变量中的函数,它从一个名为"common"的依赖项中获取它的值.
我知道我可能需要以某种方式存根这个函数,但我不确定从哪个特定的例子开始,因为我对angularjs,jasmine等等都很新.任何想法都表示赞赏
单元测试:
describe("quote", function () {
var scope,
controller,
common;
beforeEach(inject(function($rootScope, $controller, _common_) {
scope = $rootScope.$new();
common = _common_;
controller = $controller;
}));
describe("removeAttachment", function() {
it("should remove the attachment when requested", function() {
var vm = controller("quote", { $scope: scope });
vm.attachmentList.push({ FileName: "file1", FileAsString: "..." });
vm.attachmentList.push({ FileName: "file2", FileAsString: "..." });
vm.attachmentList.push({ FileName: "file3", FileAsString: "..." });
expect(vm.attachmentList.length).toEqual(3);
// here's the call where it fails
vm.removeAttachment(vm.attachmentList[1]);
expect(vm.attachmentListCount).toEqual(2);
expect(vm.attachmentList.length).toEqual(2);
expect(vm.attachmentList[1].FileName).toBe("file3");
});
}); …Run Code Online (Sandbox Code Playgroud) 我有一个名为myFactory的工厂,它具有依赖$ http.在我的测试中,我想模仿这种依赖.我发现我可以使用$ httpBackend实现它 .我使用下面的代码,它的工作原理.但我不明白为什么.在什么时候,angular知道httpBackend实际上正在替换myFactory中的$ http?
beforeEach(inject(function(_myFactory_, _$httpBackend_){
myFactory = _myFactory_;
$httpBackend = _$httpBackend_;
}));
Run Code Online (Sandbox Code Playgroud) 根据Angular docs for ngMock,$ httpBackend when方法应该拦截$ http服务请求并提供指定的响应.我假设模拟$ httpBackend方法将是同步的,以使测试更容易.但是result.test最终没有被定义.
describe('Http requests', function () {
var scope, $httpBackend, constituents;
beforeEach(module('main'));
beforeEach(inject(function (_$httpBackend_, _constituents_) {
constituents = _constituents_;
$httpBackend = _$httpBackend_;
$httpBackend.expectGET("App/Main/login.html").respond(200);
}));
it('Should get the constituents', function () {
$httpBackend.whenGET(webServicesPath + "api/constituents/all-constituents").respond(200, { "test": true });
var result = constituents.getAllConstituents();
$httpBackend.flush();
expect(result.$$state.value.test).toEqual(true);
});
});
Run Code Online (Sandbox Code Playgroud)
我尝试使用$ httpBackend.flush(),但这会产生意想不到的后果......
错误:意外请求:GET App/Main/login.html
这意味着以某种方式调用了ui.routing服务.所以我来处理,它通过添加$httpBackend.expectGET...的beforeEach.
为什么我甚至要使用flush方法? 似乎过于复杂.当与单元测试无关时,为什么会触发ui.routing?
作为参考,这是工厂使用的
app.factory('constituents', ['$http', '$log', function ($http, $log) {
function getAllConstituents() {
return $http.get(webServicesPath + "api/constituents/all-constituents").then(
function (response) …Run Code Online (Sandbox Code Playgroud) 我为AngularJS应用程序的服务编写了以下非常简单的测试:
describe('Services', function () {
beforeEach(function(){
this.addMatchers({
toEqualData: function(expected) {
return angular.equals(this.actual, expected);
}
});
});
beforeEach(module('myapp.services'));
describe('Album service', function(){
var Album;
beforeEach(inject(function (_Album_) {
Album = _Album_;
}));
it('can get an instance of the Album factory', inject(function (Album) {
expect(Album).toBeDefined();
}));
});
});
Run Code Online (Sandbox Code Playgroud)
我刚刚在AngularJS文档中建议添加了自定义toEqualData匹配器.然而,这打破了测试.我正在使用Gulp来运行测试gulp-karma(虽然如果我直接使用Karma会出现同样的问题),并且我看到以下错误消息:
$ gulp test
[10:00:04] Using gulpfile ~/Projects/myapp/gulpfile.js
[10:00:04] Starting 'jshint'...
[10:00:04] Starting 'karma'...
WARN `start` method is deprecated since 0.13. It will be removed in 0.14. Please use
server = new …Run Code Online (Sandbox Code Playgroud) 我的问题与在Windows 7/OSX上无法安装jasmine-core略有不同
我试过这个命令:
npm install karma karma-jasmine karma-phantomjs-launcher --save-dev
Run Code Online (Sandbox Code Playgroud)
我在答案中看到Karma不支持Node的v.5.3.0(它似乎与我的v.5.2.0版本相同).
但是我能做什么?引用的线程没有给出解决问题的任何答案...降级节点和我安装的每个包?如何?这管用吗?我是这些技术的新手.
以下是我得到的错误:
??? UNMET PEER DEPENDENCY jasmine-core@*
??? UNMET PEER DEPENDENCY phantomjs@>=1.9
npm WARN karma-jasmine@0.3.6 requires a peer of jasmine-core@* but none was installed.
npm WARN karma-phantomjs-launcher@0.2.2 requires a peer of phantomjs@>=1.9 but none was installed.
Run Code Online (Sandbox Code Playgroud)
另一个问题是
npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.6
Run Code Online (Sandbox Code Playgroud)
那我该怎么办?
我正在尝试编写一个评估有时候的Jasmine测试.以前我Moment.js用来计算时间,但我试图在某些地方使用Date.now()以提高速度.反正有这样做moment(time).add(1, "minutes")有Date.js?
我有一个情况.我正在测试的网页是使用AngularJS和KnockoutJS开发的.
使用Angularjs开发搜索网站中的酒店和其他页面.**
使用knockoutjs开发预订酒店和付款.
我知道Jasmine框架用于测试Knockoutjs应用程序.对于Angularjs和knockoutjs应用程序,我可以在c#中使用Protractor框架吗?
或者是否有任何其他e2e测试框架来测试此类Web应用程序?
我有一个属性disabled ='disabled'.我怎么能等待,直到元素完全停止具有该属性,不能在'启用'上更改,但根本不再具有该属性.
我正在检查属性值,如下所示:
el.getAttribute('disabled').then(function (atr) {
expect(atr).toMatch('disabled');
});
Run Code Online (Sandbox Code Playgroud)
现在我需要等待一个条件,当这个属性消失,但没有找到任何合适的解决方案.
jasmine ×10
angularjs ×5
unit-testing ×4
javascript ×3
protractor ×3
automation ×1
c# ×1
datejs ×1
gulp-karma ×1
hottowel ×1
karma-runner ×1
npm ×1
phantomjs ×1
testing ×1